- Do I need always import all I need in the beginning of the script?
- Use import immediately before using things from this?
- What to do if import thing uses several times?
What is good style of using import in Python?
What is good style of using import in Python?
I usually put my imports at the top of scripts: this way, I know my dependencies and I don't have to go through the whole script.
Importing several times the same module/package is no problem, functionally speaking, but incurs a small overhead past the first time.
When I write multiprocessing applications, I will do my imports a bit differently though: do only the required imports on one side of the fork()
and do the rest on the other side.
From the PEP8 style guide:
- Imports should usually be on separate lines, e.g.:
Yes: import os import sys
No: import sys, os
it's okay to say this though:
from subprocess import Popen, PIPE
- Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
Imports should be grouped in the following order:
- standard library imports
- related third party imports
- local application/library specific imports
You should put a blank line between each group of imports.
Put any relevant all specification after the imports.
Relative imports for intra-package imports are highly discouraged. Always use the absolute package path for all imports. Even now that PEP 328 [7] is fully implemented in Python 2.5, its style of explicit relative imports is actively discouraged; absolute imports are more portable and usually more readable.
When importing a class from a class-containing module, it's usually okay to spell this
from myclass import MyClass
from foo.bar.yourclass import YourClass
If this spelling causes local name clashes, then spell them
import myclass
import foo.bar.yourclass
and use "myclass.MyClass" and "foo.bar.yourclass.YourClass"
Do I need always import all I need in the beginning of the script?
You don't need to, but it's considered good style (like #include
statements in C/C++). It has the advantage that you don't have to scroll/search through the whole file to find imported modules.
Use import immediately before using things from this?
This is sometimes done inside functions in order to hide the import from the module namespace (or for deferred loading). But I've never found a good use case for that.
What to do if import thing uses several times?
Python takes care of that. A module won't be imported twice unless you hack sys.modules
or use reload(...)
.
In general, you should use from .localsubmodule import a, b, c
for local imports (from ..parentmodule
is also possible) and import os
or from os import path
for global imports, for example. You can look at PEP8 - Style Guide for Python Code for more examples and suggestions.
When creating modules, make sure they are from ... import
"compatible", i.e. define the __all__
variable so that you only export the objects/submodules that should be public.
There are definitely reasonable cases to lazy-load imports. Rare, but they exist. In these case, I like to find them at the top of a function that first requires an import..