In Python, what exactly does import *
import? Does it import __init__.py
found in the containing folder?
For example, is it necessary to declare from project.model import __init__
, or is from project.model import *
sufficient?
In Python, what exactly does import *
import? Does it import __init__.py
found in the containing folder?
For example, is it necessary to declare from project.model import __init__
, or is from project.model import *
sufficient?
The "advantage" of
from xyz import *
as opposed to other forms of import is that it imports everything (well, almost... [see (a) below] everything) from the designated module under the current module. This allows using the various objects (variables, classes, methods...) from the imported module without prefixing them with the module's name. For exampleThis practice (of importing * into the current namespace) is however discouraged because it
Typically we therefore limit this import * practice to ad-hoc tests and the like. As pointed out by @Denilson-Sá-Maia, some libraries such as (e.g. pygame) have a sub-module where all the most commonly used constants and functions are defined and such sub-modules are effectively designed to be imported with
import *
. Other than with these special sub-modules, it is otherwise preferable to ...:explicitly import a few objects only
or import the module under its own namespace (or an alias thereof, in particular if this is a long name, and the program references its objects many times)
See the Python documentation on this topic
(a) Specifically, what gets imported with
from xyz import *
?if xyz module defines an
__all__
variable, it will import all the names defined in this sequence, otherwise it will import all names, except these which start with an underscore.Note Many libraries have sub-modules. For example the standard library
urllib
includes sub-modules likeurllib.request
,urllib.errors
,urllib.response
etc. A common point of confusion is thatfrom urllib import *
would import all these sub-modules. That is NOT the case: one needs to explicitly imports these separately with, say,
from urllib.request import *
etc. This incidentally is not specific toimport *
, plainimport
will not import sub-modules either (but of course, the*
which is often a shorthand for "everything" may mislead people in thinking that all sub-modules and everything else would be imported).It import (into the current namespace) whatever names the module (or package) lists in its
__all__
attribute -- missing such an attribute, all names that don't start with_
.It's mostly intended as a handy shortcut for use only in interactive interpreter sessions: as other answers suggest, don't use it in a program.
My recommendation, per Google's Python style guide, is to only ever import modules, not classes or functions (or other names) from within modules. Strictly following this makes for clarity and precision, and avoids subtle traps that may come when you import "stuff from within a module".
Importing a package (or anything from inside it) intrinsically loads and executes the package's
__init__.py
-- that file defines the body of the package. However, it does not bind the name__init__
in your current namespace (so in this sense it doesn't import that name).If
project.model
is a package, the module referred to byimport project.model
is from.../project/model/__init__.py
.from project.model import *
dumps everything from__init__.py
's namespace into yours. It does not automatically do anything with the other modules in model. The preferred style is for__init__.py
not to contain anything.Never ever ever ever ever use
import *
. It makes your code unreadable and unmaintainable.If the module in question (
project.model
in your case) has defined a list of stings named__all__
, then every named variable in that list is imported. If there is no such variable, it imports everything.Yes, it does. It imports everything (that is not a private variable, i.e.: variables whose names start with
_
or__
), and you should try not to use it according to "Properly importing modules in Python" to avoid polluting the local namespace.It is enough, but generally you should either do
import project.model
, which already imports__init__.py
, per "Understanding python imports", but can get too wordy if you use it too much, orimport project.model as pm
orimport project.model as model
to save a few keystrokes later on when you use it.Follow Alex's advice in "What exactly does "import *" import?"