0

I am writing a python module and I am using many imports of other different modules. I am bit confused that whether I should import all the necessary dependent modules in the opening of the file or shall I do it when necessary.

I also wanted to know the implications of both.

I come from C++ back ground so I am really thrilled with this feature and does not see any reason of not using __import__(), importing the modules only when needed inside my function.

Kindly throw some light on this.

mgilson
  • 300,191
  • 65
  • 633
  • 696
Bhupesh Pant
  • 4,053
  • 5
  • 45
  • 70
  • Are you talking about importing things inside functions? Because that has nothing to do with `__import__`, you can do that with `import` too. In that case, this is a duplicate of http://stackoverflow.com/q/5262406/395760 –  Feb 21 '14 at 11:50

3 Answers3

1

To write less code, import a module at the first lines of the script, e.g.:

#File1.py
import os

#use os somewhere:
os.path.chdir(some_dir)
...
...
#use os somewhere else, you don't need to "import os" everywhere
os.environ.update(some_dict)

While sometimes you may need to import a module locally (e.g., in a function):

abc=3
def foo():
  from some_module import abc #import inside foo avoids you from naming conflicts
  abc(...) #call the function, nothing to do with the variable "abc" outside "foo"

Don't worry about the time consumption when calling foo() multiple times, since import statements loads modules/functions only one time. Once a module/function is imported, the object is stored in dictionary sys.modules, which is a lookup table for speedup when running the same import statement.

As @bruno desthuilliers mentioned, importing insede functions may not be that pythonic, it violates PEP8, here's a discussion I found, you should stick to importing at the top of the file most of the time.

Community
  • 1
  • 1
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
1

First, __import__ isn't usually needed anywhere. It's main purpose is to support dynamic importing of things that you don't know ahead of time (think plug-ins). You can easily use the import statement inside your function:

import sys

def foo():
    import this

if __name__ == "__main__":
    print sys.version_info
    foo()

The main advantage to importing everything up-front is that it is most customary. That's where people reading your code will go to see if something is imported or not. Also, you don't need to write import os in every function that uses os. The main downsides of this approach are that:

  • you can get yourself into unresolvable import loops (A imports B which imports A)
  • that you pull everything into memory even if you aren't going to use it.

The second problem isn't typically an issue -- very rarely do you notice the performance or memory impact of an import.

If you run into the first problem, it's likely a symptom of poorly grouped code and the common stuff should be factored into a new module C which both A and B can use.

mgilson
  • 300,191
  • 65
  • 633
  • 696
0

Firstly, it's a violation of PEP8 using imports inside functions. Calling import it's an expensive call EVEN if the module is already loaded, so if your function is gonna being called many times this will not compensate the performance gain.

Also when you call "import test" python do this:
dataFile = __ import__('test')

The only downside of imports at the top of file it's the namespace that get polluted very fast depending on complexity of the file, but if your file it's too complex it's a signal of bad design.

Frank Vieira
  • 45
  • 1
  • 6