Importing modules
The all special variable
Section titled “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:
__all__ = ['imported_by_star']
imported_by_star = 42not_imported_by_star = 21Only imported_by_star is imported when using from mymodule import *:
>>> from mymodule import *>>> imported_by_star42>>> not_imported_by_starTraceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name 'not_imported_by_star' is not definedHowever, not_imported_by_star can be imported explicitly:
>>> from mymodule import not_imported_by_star>>> not_imported_by_star21Importing a module
Section titled “Importing a module”Use the import statement:
>>> import random>>> print(random.randint(1, 10))4import 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 randint with random.randint.
You can import a module and assign it to a different name:
>>> import random as rn>>> print(rn.randint(1, 10))4If your python file main.py is in the same folder as custom.py. You can import it like this:
import customIt is also possible to import a function from a module:
>>> from math import sin>>> sin(1)0.8414709848078965To import specific functions deeper down into a module, the dot operator may be used only on the left side of the import keyword:
from urllib.request import urlopenIn 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 function. Then import statement will work good.
from hello import functionfrom world import function
function() #world's function will be invoked. Not hello'sIn general import will provide you a namespace.
import helloimport world
hello.function() # exclusively hello's function will be invokedworld.function() # exclusively world's function will be invokedBut if you are sure enough, in your whole project there is no way having same function name you should use from statement
Multiple imports can be made on the same line:
>>> # Multiple modules>>> import time, sockets, random>>> # Multiple functions>>> from math import sin, cos, tan>>> # Multiple constants>>> from math import pi, e
>>> print(pi)3.141592653589793>>> print(cos(45))0.5253219888177297>>> print(time.time())1482807222.7240417The keywords and syntax shown above can also be used in combinations:
>>> from urllib.request import urlopen as geturl, pathname2url as path2url, getproxies>>> from math import factorial as fact, gamma, atan as arctan>>> import random.randint, time, sys
>>> print(time.time())1482807222.7240417>>> print(arctan(60))1.554131203080956>>> filepath = "/dogs/jumping poodle (december).png">>> print(path2url(filepath))/dogs/jumping%20poodle%20%28december%29.pngImport modules from an arbitrary filesystem location
Section titled “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.
import syssys.path.append("/path/to/directory/containing/your/module")import mymoduleIt 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
Section titled “Importing all names from a module”from module_name import *for example:
from math import *sqrt(2) # instead of math.sqrt(2)ceil(2.7) # instead of math.ceil(2.7)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:
def sqrt(num): print("I don't know what's the square root of {}.".format(num))
sqrt(4)# Output: I don't know what's the square root of 4.
from math import *sqrt(4)# Output: 2.0Starred imports are only allowed at the module level. Attempts to perform them in class or function definitions result in a SyntaxError.
def f(): from math import *and
class A: from math import *both fail with:
SyntaxError: import * only allowed at module levelProgrammatic importing
Section titled “Programmatic importing”To import a module through a function call, use the importlib module (included in Python starting in version 2.7):
import importlibrandom = importlib.import_module("random")The importlib.import_module() function will also import the submodule of a package directly:
collections_abc = importlib.import_module("collections.abc")For older versions of Python, use the imp module.
Use the functions imp.find_module and imp.load_module to perform a programmatic import.
Taken from standard library documentation
import imp, sysdef import_module(name): fp, pathname, description = imp.find_module(name) try: return imp.load_module(name, fp, pathname, description) finally: if fp: fp.close()Do NOT use __import__() to programmatically import modules! There are subtle details involving sys.modules, the fromlist argument, etc. that are easy to overlook which importlib.import_module() handles for you.
PEP8 rules for Imports
Section titled “PEP8 rules for Imports”Some recommended PEP8 style guidelines for imports:
from math import sqrt, ceil # Not recommended from math import sqrt # Recommended from math import ceil1. Standard library imports 1. Related third party imports 1. Local application/library specific imports
Importing specific names from a module
Section titled “Importing specific names from a module”Instead of importing the complete module you can import only specified names:
from random import randint # Syntax "from MODULENAME import NAME1[, NAME2[, ...]]"print(randint(1, 10)) # Out: 5from 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):
from math import piprint(pi) # Out: 3.14159265359The following example will raise an error, because we haven’t imported a module:
random.randrange(1, 10) # works only if "import random" has been run beforeOutputs:
NameError: name 'random' is not definedThe python interpreter does not understand what you mean with random. It needs to be declared by adding import random to the example:
import randomrandom.randrange(1, 10)Importing submodules
Section titled “Importing submodules”from module.submodule import functionThis imports function from module.submodule.
import() function
Section titled “import() function”The __import__() function can be used to import modules where the name is only known at runtime
if user_input == "os": os = __import__("os")
# equivalent to import osThis function can also be used to specify the file path to a module
mod = __import__(r"C:/path/to/file/anywhere/on/computer/module.py")Re-importing a module
Section titled “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:
import mathmath.pi = 3print(math.pi) # 3import mathprint(math.pi) # 3This 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:
print(math.pi) # 3import sysif 'math' in sys.modules: # Is the ``math`` module in the register? del sys.modules['math'] # If so, remove it.import mathprint(math.pi) # 3.141592653589793But there is more a straightforward and simple way.
Python 2
Section titled “Python 2”Use the reload function:
import mathmath.pi = 3print(math.pi) # 3reload(math)print(math.pi) # 3.141592653589793Python 3
Section titled “Python 3”The reload function has moved to importlib:
import mathmath.pi = 3print(math.pi) # 3from importlib import reloadreload(math)print(math.pi) # 3.141592653589793Syntax
Section titled “Syntax”- 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]
Remarks
Section titled “Remarks”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.