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
If the imported module is infrequently used and the import is expensive, the in-the-middle-import is OK.
What happens if a module is imported twice? The module is only loaded the first time the import statement is executed and there is no performance loss by importing it again. You can examine sys. modules to find out which modules have already been loaded.
Using import * in python programs is considered a bad habit because this way you are polluting your namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import.
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 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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With