Say I have two Python modules:
module1.py
:
import module2
def myFunct(): print "called from module1"
module2.py
:
def myFunct(): print "called from module2"
def someFunct(): print "also called from module2"
If I import module1
, is it better etiquette to re-import module2
, or just refer to it as module1.module2
?
For example (someotherfile.py
):
import module1
module1.myFunct() # prints "called from module1"
module1.module2.myFunct() # prints "called from module2"
I can also do this: module2 = module1.module2
. Now, I can directly call module2.myFunct()
.
However, I can change module1.py
to:
from module2 import *
def myFunct(): print "called from module1"
Now, in someotherfile.py
, I can do this:
import module1
module1.myFunct() # prints "called from module1"; overrides module2
module1.someFunct() # prints "also called from module2"
Also, by importing *
, help('module1') shows all of the functions from module2
.
On the other hand, (assuming module1.py
uses import module2
), I can do:
someotherfile.py
:
import module1, module2
module1.myFunct() # prints "called from module1"
module2.myFunct() # prints "called from module2"
Again, which is better etiquette and practice? To import module2
again, or to just refer to module1
's importation?
To use the module, you have to import it using the import keyword. The function or variables present inside the file can be used in another file by importing the module.
Import order does not matter. If a module relies on other modules, it needs to import them itself. Python treats each . py file as a self-contained unit as far as what's visible in that file.
Imports should always be written at the top of the file, after any module comments and docstrings. Imports should be divided according to what is being imported. There are generally three groups: standard library imports (Python's built-in modules)
Quoting the PEP 8 style guide:
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
Emphasis mine.
Don't use module1.module2
; you are relying on the internal implementation details of module1
, which may later change what imports it is using. You can import module2
directly, so do so unless otherwise documented by the module author.
You can use the __all__
convention to limit what is imported from a module with from modulename import *
; the help()
command honours that list as well. Listing the names you explicitly export in __all__
helps clean up the help()
text presentation:
The public names defined by a module are determined by checking the module’s namespace for a variable named
__all__
; if defined, it must be a sequence of strings which are names defined or imported by that module. The names given in__all__
are all considered public and are required to exist. If__all__
is not defined, the set of public names includes all names found in the module’s namespace which do not begin with an underscore character ('_'
).__all__
should contain the entire public API. It is intended to avoid accidentally exporting items that are not part of the API (such as library modules which were imported and used within the module).
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