Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Optimizing imports

Tags:

python

import

Does it matter where modules are loaded in a code? Or should they all be declared at the top, since during load time the external modules will have to be loaded regardless of where they are declared in the code...?

Example:

from os import popen

try:
    popen('echo hi')
    doSomethingIllegal;
except:
    import logging                   #Module called only when needed?
    logging.exception("Record to logger)

or is this optimized by the compiler the same way as:

from os import popen
import logging                      #Module will always loaded regardless

try:
    popen('echo hi')
    doSomethingIllegal;
except:
    logging.exception("Record to logger)
like image 905
tetris11 Avatar asked May 20 '12 10:05

tetris11


2 Answers

This indicates it may make a difference:

"import statements can be executed just about anywhere. It's often useful to place them inside functions to restrict their visibility and/or reduce initial startup time. Although Python's interpreter is optimized to not import the same module multiple times, repeatedly executing an import statement can seriously affect performance in some circumstances."

These two OS questions, local import statements? and import always at top of module? discuss this at length.

Finally, if you are curious about your specific case you could profile/benchmark your two alternatives in your environment.

I prefer to put all of my import statements at the top of the source file, following stylistic conventions and for consistency (also it would make changes easier later w/o having to hunt through the source file looking for import statements scattered throughout)

like image 80
Levon Avatar answered Sep 22 '22 22:09

Levon


The general rule of thumb is that imports should be at the top of the file, as that makes code easier to follow, and that makes it easier to figure out what a module will need without having to go through all the code.

The Python style guide covers some basic guidelines for how imports should look: http://www.python.org/dev/peps/pep-0008/#imports

In practice, though, there are times when it makes sense to import from within a particular function. This comes up with imports that would be circular:

# Module 1
from module2 import B

class A(object):
    def do_something(self):
        my_b = B()
        ...

# Module 2
from module1 import A

class B(object):
    def do_something(self):
        my_a = A()
        ...

That won't work as is, but you could get around the circularity by moving the import:

# Module 1
from module2 import B

class A(object):
    def do_something(self):
        my_b = B()
        ...

# Module 2
class B(object):
    def do_something(self):
        from module1 import A
        my_a = A()
        ...

Ideally, you would design the classes such that this would never come up, and maybe even include them in the same module. In that toy example, having each import the other really doesn't make sense. However, in practice, there are some cases where it makes more sense to include an import for one method within the method itself, rather than throwing everything into the same module, or extracting the method in question out to some other object.

But, unless you have good reason to deviate, I say go with the top-of-the-module convention.

like image 44
Karmel Avatar answered Sep 19 '22 22:09

Karmel