- import module_name
- import module_name.submodule_name
- from module_name import *
- from module_name import submodule_name [, class_name, function_name, ...etc]
- from module_name import some_name as new_name
- from module_name.submodule_name import class_name [, function_name, ...etc]
Importing a module will make Python evaluate all top-level code in this module so it learns all the functions, classes, and variables that the module contains. When you want a module of yours to be imported somewhere else, be careful with your top-level code, and encapsulate it into
if __name__ == '__main__': if you don't want it to be executed when the module gets imported.
Importing a module
import module will import a module and then allow you to reference its objects -- values, functions and classes, for example -- using the
module.name syntax. In the above example, the
random module is imported, which contains the
randint function. So by importing
random you can call
You can import a module and assign it to a different name:
If your python file
main.py is in the same folder as
custom.py. You can import it like this:
It is also possible to import a function from a module:
To import specific functions deeper down into a module, the dot operator may be used only on the left side of the
In python, we have two ways to call function from top level. One is
import and another is
from. We should use
import when we have a possibility of name collision. Suppose we have
hello.py file and
world.py files having same function named
import statement will work good.
import will provide you a namespace.
But if you are sure enough, in your whole project there is no way having same function name you should use
Multiple imports can be made on the same line:
The keywords and syntax shown above can also be used in combinations:
__import__() function can be used to import modules where the name is only known at runtime
This function can also be used to specify the file path to a module
Import modules from an arbitrary filesystem location
If you want to import a module that doesn't already exist as a built-in module in the Python Standard Library nor as a side-package, you can do this by adding the path to the directory where your module is found to
sys.path. This may be useful where multiple python environments exist on a host.
It is important that you append the path to the directory in which
mymodule is found, not the path to the module itself.
Importing all names from a module
This will import all names defined in the
math module into the global namespace, other than names that begin with an underscore (which indicates that the writer feels that it is for internal use only).
Warning: If a function with the same name was already defined or imported, it will be overwritten. Almost always importing only specific names
from math import sqrt, ceil is the recommended way:
Starred imports are only allowed at the module level. Attempts to perform them in class or function definitions result in a
both fail with:
Importing specific names from a module
Instead of importing the complete module you can import only specified names:
from random is needed, because the python interpreter has to know from which resource it should import a function or class and
import randint specifies the function or class itself.
Another example below (similar to the one above):
The following example will raise an error, because we haven't imported a module:
The python interpreter does not understand what you mean with
random. It needs to be declared by adding
import random to the example:
PEP8 rules for Imports
Some recommended PEP8 style guidelines for imports:
Imports should be on separate lines:
Order imports as follows at the top of the module:
- Standard library imports
- Related third party imports
- Local application/library specific imports
Wildcard imports should be avoided as it leads to confusion in names in the current namespace. If you do
from module import *, it can be unclear if a specific name in your code comes from
moduleor not. This is doubly true if you have multiple
from module import *-type statements.
Avoid using relative imports; use explicit imports instead.
To import a module through a function call, use the
importlib module (included in Python starting in version 2.7):
importlib.import_module() function will also import the submodule of a package directly:
For older versions of Python, use the
Use the functions
imp.load_module to perform a programmatic import.
Taken from standard library documentation
Do NOT use
__import__() to programmatically import modules! There are subtle details involving
fromlist argument, etc. that are easy to overlook which
importlib.import_module() handles for you.
Re-importing a module
When using the interactive interpreter, you might want to reload a module. This can be useful if you're editing a module and want to import the newest version, or if you've monkey-patched an element of an existing module and want to revert your changes.
Note that you can't just
import the module again to revert:
This is because the interpreter registers every module you import. And when you try to reimport a module, the interpreter sees it in the register and does nothing. So the hard way to reimport is to use
import after removing the corresponding item from the register:
But there is more a straightforward and simple way.
reload function has moved to
The __all__ special variable
Modules can have a special variable named
__all__ to restrict what variables are imported when using
from mymodule import *.
Given the following module:
imported_by_star is imported when using
from mymodule import *:
not_imported_by_star can be imported explicitly: