I've tried to find a comprehensive guide on whether it is best to use import module
or from module import
? I've just started with Python and I'm trying to start off with best practices in mind.
Basically, I was hoping if anyone could share their experiences, what preferences other developers have and what's the best way to avoid any gotchas down the road?
Both ways are supported for a reason: there are times when one is more appropriate than the other.
import module
: nice when you are using many bits from the module. drawback is that you'll need to qualify each reference with the module name.from module import ...
: nice that imported items are usable directly without module name prefix. The drawback is that you must list each thing you use, and that it's not clear in code where something came from.Which to use depends on which makes the code clear and readable, and has more than a little to do with personal preference. I lean toward
import module
generally because in the code it's very clear where an object or function came from. I usefrom module import ...
when I'm using some object/function a lot in the code.Here is another difference not mentioned. This is copied verbatim from http://docs.python.org/2/tutorial/modules.html
Note that when using
the item can be either a submodule (or subpackage) of the package, or some other name defined in the package, like a function, class or variable. The import statement first tests whether the item is defined in the package; if not, it assumes it is a module and attempts to load it. If it fails to find it, an ImportError exception is raised.
Contrarily, when using syntax like
each item except for the last must be a package; the last item can be a module or a package but can’t be a class or function or variable defined in the previous item.
I personally always use
and then access everything as
etc. The reason is that at the same time you have short invocation, and you clearly define the module namespace of each routine, something that is very useful if you have to search for usage of a given module in your source.
Needless to say, do not use the import *, because it pollutes your namespace and it does not tell you where a given function comes from (from which module)
Of course, you can run in trouble if you have the same module name for two different modules in two different packages, like
in this case, of course you run into troubles, but then there's a strong hint that your package layout is flawed, and you have to rethink it.
Import Module - You don't need additional efforts to fetch another thing from module. It has disadvantages such as redundant typing
Module Import From - Less typing &More control over which items of a module can be accessed.To use a new item from the module you have to update your import statement.
Is best when you will use many functions from the module.
Is best when you want to avoid polluting the global namespace with all the functions and types from a module when you only need
function
.I've just discovered one more subtle difference between these two methods.
If module
foo
uses a following import:Then module
bar
can by mistake usecount
as though it was defined infoo
, not initertools
:If
foo
uses:the mistake is still possible, but less likely to be made.
bar
needs to:This caused some troubles to me. I had a module that by mistake imported an exception from a module that did not define it, only imported it from other module (using
from module import SomeException
). When the import was no longer needed and removed, the offending module was broken.