Good or bad practice in Python: import in the middle of a file
Question:
Suppose I have a relatively long module, but need an external module or method only once.
Is it considered OK to import that method or module in the middle of the module?
Or should import
s only be in the first part of the module.
Example:
import string, pythis, pythat
...
...
...
...
def func():
blah
blah
blah
from pysomething import foo
foo()
etc
etc
etc
...
...
...
Please justify your answer and add links to PEPs or relevant sources
Answers:
It is considered “Good Form” to group all imports together at the start of the file.
Modules can import other modules. It is customary but not required to place all import statements at the beginning of a module (or script, for that matter). The imported module names are placed in the importing module’s global symbol table.
From here: http://docs.python.org/tutorial/modules.html
It’s generally considered bad practice, but sometimes it’s unavoidable (say when you have to avoid a circular import).
An example of a time when it is necessary: I use Waf to build all our code. The system is split into tools, and each tool is implemented in it’s own module. Each tool module can implent a detect()
method to detect if the pre-requisites are present. An example of one of these may do the following:
def detect(self):
import foobar
If this works correctly, the tool is usable. Then later in the same module the foobar
module may be needed, so you would have to import it again, at function level scope. Clearly if it was imported at module level things would blow up completely.
PEP 8 authoritatively states:
Imports are always put at the top of
the file, just after any module
comments and docstrings, and before module globals and constants.
PEP 8 should be the basis of any “in-house” style guide, since it summarizes what the core Python team has found to be the most effective style, overall (and with individual dissent of course, as on any other language, but consensus and the BDFL agree on PEP 8).
If the imported module is infrequently used and the import is expensive, the in-the-middle-import is OK.
Otherwise, is it wise to follow Alex Martelli’s suggestion.
PEP8:
Imports are always put at the top of
the file, just after any module
comments and docstrings, and before module globals and constants.
It is not bad practice to have scopped imports. So that the import applies only to the function you used it in.
I think the code would be more readable though if the imports where grouped together at the top of the block or if you want it globally at the top of the file.
Well, I think it is a good practice to group all imports together at start of file since everyone knows where to look if want to know which libs are loaded
There was a detailed discussion of this topic on the Python mailing list in 2001:
https://mail.python.org/pipermail/python-list/2001-July/071567.html
Here are some of the reasons discussed in that thread. From Peter Hansen, here are three reasons not to have imports all at the top of the file:
Possible reasons to import in a function:
-
Readability: if the import is needed in only one
function and that’s very unlikely ever to change,
it might be clearer and cleaner to put it there only.
-
Startup time: if you don’t have the import outside
of the function definitions, it will not execute
when your module is first imported by another, but
only when one of the functions is called. This
delays the overhead of the import (or avoids it
if the functions might never be called).
-
There is always one more reason than the ones
we’ve thought of until now.
Just van Rossum chimed in with a fourth:
- Overhead: if the module imports a lot of modules,
and there’s a good chance only a few will actually
be used. This is similar to the “Startup time”
reason, but goes a little further. If a script
using your module only uses a small subset of the
functionality it can save quite some time, especially
if the imports that can be avoided also import a lot
of modules.
A fifth was offered as local imports are a way to avoid the problem of circular imports.
Feel free to read through that thread for the full discussion.
95% of the time, you should put all your imports at the top of the file. One case where you might want to do a function-local import is if you have to do it in order to avoid circular imports. Say foo.py imports bar.py, and a function in bar.py needs to import something from foo.py. If you put all your imports at the top, you could have unexpected problems importing files that rely on information that hasn’t been compiled yet. In this case, having a function local import can allow your code to hold off on importing the other module until its code has been fully compiled, and you call the relevant function.
However, it looks like your use-case is more about making it clear where foo() is coming from. In this case, I would far prefer one of two things:
First, rather than
from prerequisite import foo
import prerequisite directly, and later on refer to it as prerequisite.foo. The added verbosity pays itself back in spades through increased code transparency.
Alternatively, (or in conjunction with the above) if it’s really such a long distance between your import and the place it’s being used, it may be that your module is too big. The need for an import that nothing else uses might be an indication of a place where your code could stand to be refactored into a more manageably-sized chunk.
Everyone else has already mentioned the PEPs, but also take care to not have import statements in the middle of critical code. At least under Python 2.6, there are several more bytecode instructions required when a function has an import statement.
>>> def f():
from time import time
print time()
>>> dis.dis(f)
2 0 LOAD_CONST 1 (-1)
3 LOAD_CONST 2 (('time',))
6 IMPORT_NAME 0 (time)
9 IMPORT_FROM 0 (time)
12 STORE_FAST 0 (time)
15 POP_TOP
3 16 LOAD_FAST 0 (time)
19 CALL_FUNCTION 0
22 PRINT_ITEM
23 PRINT_NEWLINE
24 LOAD_CONST 0 (None)
27 RETURN_VALUE
>>> def g():
print time()
>>> dis.dis(g)
2 0 LOAD_GLOBAL 0 (time)
3 CALL_FUNCTION 0
6 PRINT_ITEM
7 PRINT_NEWLINE
8 LOAD_CONST 0 (None)
11 RETURN_VALUE
Suppose I have a relatively long module, but need an external module or method only once.
Is it considered OK to import that method or module in the middle of the module?
Or should import
s only be in the first part of the module.
Example:
import string, pythis, pythat
...
...
...
...
def func():
blah
blah
blah
from pysomething import foo
foo()
etc
etc
etc
...
...
...
Please justify your answer and add links to PEPs or relevant sources
It is considered “Good Form” to group all imports together at the start of the file.
Modules can import other modules. It is customary but not required to place all import statements at the beginning of a module (or script, for that matter). The imported module names are placed in the importing module’s global symbol table.
From here: http://docs.python.org/tutorial/modules.html
It’s generally considered bad practice, but sometimes it’s unavoidable (say when you have to avoid a circular import).
An example of a time when it is necessary: I use Waf to build all our code. The system is split into tools, and each tool is implemented in it’s own module. Each tool module can implent a detect()
method to detect if the pre-requisites are present. An example of one of these may do the following:
def detect(self):
import foobar
If this works correctly, the tool is usable. Then later in the same module the foobar
module may be needed, so you would have to import it again, at function level scope. Clearly if it was imported at module level things would blow up completely.
PEP 8 authoritatively states:
Imports are always put at the top of
the file, just after any module
comments and docstrings, and before module globals and constants.
PEP 8 should be the basis of any “in-house” style guide, since it summarizes what the core Python team has found to be the most effective style, overall (and with individual dissent of course, as on any other language, but consensus and the BDFL agree on PEP 8).
If the imported module is infrequently used and the import is expensive, the in-the-middle-import is OK.
Otherwise, is it wise to follow Alex Martelli’s suggestion.
PEP8:
Imports are always put at the top of
the file, just after any module
comments and docstrings, and before module globals and constants.
It is not bad practice to have scopped imports. So that the import applies only to the function you used it in.
I think the code would be more readable though if the imports where grouped together at the top of the block or if you want it globally at the top of the file.
Well, I think it is a good practice to group all imports together at start of file since everyone knows where to look if want to know which libs are loaded
There was a detailed discussion of this topic on the Python mailing list in 2001:
https://mail.python.org/pipermail/python-list/2001-July/071567.html
Here are some of the reasons discussed in that thread. From Peter Hansen, here are three reasons not to have imports all at the top of the file:
Possible reasons to import in a function:
Readability: if the import is needed in only one
function and that’s very unlikely ever to change,
it might be clearer and cleaner to put it there only.Startup time: if you don’t have the import outside
of the function definitions, it will not execute
when your module is first imported by another, but
only when one of the functions is called. This
delays the overhead of the import (or avoids it
if the functions might never be called).There is always one more reason than the ones
we’ve thought of until now.
Just van Rossum chimed in with a fourth:
- Overhead: if the module imports a lot of modules,
and there’s a good chance only a few will actually
be used. This is similar to the “Startup time”
reason, but goes a little further. If a script
using your module only uses a small subset of the
functionality it can save quite some time, especially
if the imports that can be avoided also import a lot
of modules.
A fifth was offered as local imports are a way to avoid the problem of circular imports.
Feel free to read through that thread for the full discussion.
95% of the time, you should put all your imports at the top of the file. One case where you might want to do a function-local import is if you have to do it in order to avoid circular imports. Say foo.py imports bar.py, and a function in bar.py needs to import something from foo.py. If you put all your imports at the top, you could have unexpected problems importing files that rely on information that hasn’t been compiled yet. In this case, having a function local import can allow your code to hold off on importing the other module until its code has been fully compiled, and you call the relevant function.
However, it looks like your use-case is more about making it clear where foo() is coming from. In this case, I would far prefer one of two things:
First, rather than
from prerequisite import foo
import prerequisite directly, and later on refer to it as prerequisite.foo. The added verbosity pays itself back in spades through increased code transparency.
Alternatively, (or in conjunction with the above) if it’s really such a long distance between your import and the place it’s being used, it may be that your module is too big. The need for an import that nothing else uses might be an indication of a place where your code could stand to be refactored into a more manageably-sized chunk.
Everyone else has already mentioned the PEPs, but also take care to not have import statements in the middle of critical code. At least under Python 2.6, there are several more bytecode instructions required when a function has an import statement.
>>> def f():
from time import time
print time()
>>> dis.dis(f)
2 0 LOAD_CONST 1 (-1)
3 LOAD_CONST 2 (('time',))
6 IMPORT_NAME 0 (time)
9 IMPORT_FROM 0 (time)
12 STORE_FAST 0 (time)
15 POP_TOP
3 16 LOAD_FAST 0 (time)
19 CALL_FUNCTION 0
22 PRINT_ITEM
23 PRINT_NEWLINE
24 LOAD_CONST 0 (None)
27 RETURN_VALUE
>>> def g():
print time()
>>> dis.dis(g)
2 0 LOAD_GLOBAL 0 (time)
3 CALL_FUNCTION 0
6 PRINT_ITEM
7 PRINT_NEWLINE
8 LOAD_CONST 0 (None)
11 RETURN_VALUE