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?
To add to what people have said about
from x import *
: besides making it more difficult to tell where names came from, this throws off code checkers like Pylint. They will report those names as undefined variables.With
import
, the token must be a module (a file containing Python commands) or a package (a folder in thesys.path
containing a file__init__.py
.)When there are subpackages:
the requirements for folder (package) or file (module) are the same, but the folder or file must be inside
package2
which must be insidepackage1
, and bothpackage1
andpackage2
must contain__init__.py
files. https://docs.python.org/2/tutorial/modules.htmlWith the
from
style of import:the package or module enters the namespace of the file containing the
import
statement asmodule
(orpackage
) instead ofpackage1.package2.module
. You can always bind to a more convenient name:Only the
from
style of import permits you to name a particular function or variable:is allowed, but
is not allowed.
There's another detail here, not mentioned, related to writing to a module. Granted this may not be very common, but I've needed it from time to time.
Due to the way references and name binding works in Python, if you want to update some symbol in a module, say foo.bar, from outside that module, and have other importing code "see" that change, you have to import foo a certain way. For example:
module foo:
module a:
module b:
However, if you import symbol names instead of module names, this will not work.
For example, if I do this in module a:
No code outside of a will see bar as "oranges" because my setting of bar merely affected the name "bar" inside module a, it did not "reach into" the foo module object and update its "bar".
My own answer to this depends mostly on first, how many different modules I'll be using. If i'm only going to use one or two, I'll often use
from
...import
since it makes for fewer keystrokes in the rest of the file, but if I'm going to make use of many different modules, I prefer justimport
because that means that each module reference is self-documenting. I can see where each symbol comes from without having to hunt around.Usuaully I prefer the self documenting style of plain import and only change to from.. import when the number of times I have to type the module name grows above 10 to 20, even if there's only one module being imported.
Since I am also a beginner, I will be trying to explain this in a simple way: In Python, we have three types of
import
statements which are:1. Generic imports:
this type of import is my personal favorite, the only downside to this import technique is that if you need use any module's function you must use the following syntax:
of course, it increases the typing effort but as a beginner, it will help you to keep track of module and function associated with it, (a good text editor will reduce the typing effort significantly and is recommended).
Typing effort can be further reduced by using this import statement:
now, instead of using
math.sqrt()
you can usem.sqrt()
.2. Function imports:
this type of import is best suited if your code only needs to access single or few functions from the module, but for using any new item from the module you have to update import statement.
3. Universal imports:
Although it reduces typing effort significantly but is not recommended because it will fill your code with various functions from the module and their name could conflict with the name of user-defined functions. example:
If you have a function of your very own named sqrt and you import math, your function is safe: there is your sqrt and there is math.sqrt. If you do from math import *, however, you have a problem: namely, two different functions with the exact same name. Source: Codecademy
The difference between
import module
andfrom module import foo
is mainly subjective. Pick the one you like best and be consistent in your use of it. Here are some points to help you decide.import module
import
statements. Don't need to add any additional imports to start using another item from the modulemodule.foo
in your code can be tedious and redundant (tedium can be minimized by usingimport module as mo
then typingmo.foo
)from module import foo
foo
import
statementfoo
. For example, it's less clear whatceil()
does compared tomath.ceil()
Either method is acceptable, but don't use
from module import *
.For any reasonable large set of code, if you
import *
you will likely be cementing it into the module, unable to be removed. This is because it is difficult to determine what items used in the code are coming from 'module', making it easy to get to the point where you think you don't use theimport
any more but it's extremely difficult to be sure.