What are good rules of thumb for Python imports

0 votes

I am a little confused by the ways in which you import modules in Python.

import X
import X as Y
from A import B

Should imports happen at a module level or a method/function level? Or is it in the __init__.py or in the module code itself?

Nov 14, 2018 in Python by Anirudh
• 2,090 points

edited Dec 18, 2018 by Anirudh 1,031 views

1 answer to this question.

0 votes

I would normally use import X on module level. If you only need a single object from a module, use from X import Y.

Only use import X as Y in case you're otherwise confronted with a name clash or something.

Try something like this:

def main():
  import sys
  if len(sys.argv) > 1:
     pass
answered Nov 14, 2018 by Nymeria
• 3,560 points

edited Dec 18, 2018 by Nymeria

Related Questions In Python

0 votes
2 answers

What are the types of dictionary in python?

There are 4 types of dictionary Empty Integer Mixed Dictionary with ...READ MORE

answered Feb 14, 2019 in Python by Shashank
• 1,370 points
1,044 views
0 votes
1 answer

What are the key features of Python?

If it makes for an introductory language ...READ MORE

answered Jul 20, 2018 in Python by Priyaj
• 58,020 points
660 views
+1 vote
3 answers

What are the ways of detecting outliners in Python

code from http://eurekastatistics.com/using-the-median-absolute-deviation-to-find-outliers  This uses the L1 distance ...READ MORE

answered Aug 24, 2018 in Python by eatcodesleeprepeat
• 4,710 points

reshown Aug 24, 2018 by Priyaj 1,387 views
0 votes
1 answer

What are the utiliy of python Language

At the very begining Python started as ...READ MORE

answered Oct 15, 2018 in Python by Priyaj
• 58,020 points
636 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,470 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,128 views
0 votes
1 answer

What is the equivalent of NotImplementedError using Python?

Hi, good question! One simple answer to your ...READ MORE

answered Jan 17, 2019 in Python by Nymeria
• 3,560 points
2,350 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP