Reputation: 17532
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?
Upvotes: 6
Views: 597
Reputation: 879749
Just import module2
. Re-importing is relatively costless, since Python caches module objects in sys.modules.
Moreover, chaining dots as in module1.module2.myFunct
is a violation of the Law of Demeter. Perhaps some day you will want to replace module1
with some other module module1a
which does not import module2
. By using import module2
, you will avoid having to rewrite all occurrences of module1.module2.myFunct
.
from module2 import *
is generally a bad practice since it makes it hard to trace where variables come from. And mixing module namespaces can create variable-name conflicts. For example, from numpy import *
is a definite no-no, since doing so would override Python's builtin sum
, min
, max
, any
, all
, abs
and round
.
Upvotes: 2
Reputation: 1122342
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).
Upvotes: 3