Python Reference ยท Statements and Keywords
The import statement
Reference for Python import and from ... import: load modules, rename with as, import names directly, relative imports, caching and circular import traps.
The import statement loads a module and binds it to a name in the current namespace. The first time a module is imported, Python finds it, runs its code once, and stores the resulting module object in sys.modules. Every later import of the same module just reuses that object.
The from form pulls specific names out of a module so you can use them without a prefix. Both forms accept as to give the result a different name.
import math from collections import Counter
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
import module import package.module import module as alias from module import name from module import name1, name2 as alias from module import (name1, name2) from . import sibling from ..package import name from module import *
| Form | Description |
|---|---|
import a.b.c |
Imports the whole chain and binds only the top name a. Use a.b.c.thing. |
import a.b as x |
Binds x directly to the submodule a.b. |
from m import n |
Binds n to the attribute n of module m. |
from . import x |
Relative import, valid only inside a package. One dot is the current package, two the parent. |
from m import * |
Imports all public names (those in __all__, or without a leading underscore). Allowed only at module level. |
Raises: ModuleNotFoundError when the module cannot be found; ImportError when the module exists but the requested name does not.
Usage notes
- Runs once. Top-level code in a module executes only on first import. Use
importlib.reload()to run it again (rarely needed). - Prefer explicit imports.
from m import *makes it unclear where names come from and can overwrite existing ones; avoid it outside interactive sessions. - Conventional order (PEP 8): standard library, third-party, then local imports, each group separated by a blank line, normally at the top of the file.
- Circular imports. If module A imports B and B imports A, one of them sees a half-initialized module. Fix by moving the import into a function, restructuring, or importing the module instead of names.
- Your own file names matter. A local
random.pyorjson.pyshadows the standard library module of the same name. - Search path. Modules are found through
sys.path: the script's folder,PYTHONPATH, then installed packages. - Lazy imports inside functions speed up start-up and can break cycles, at the cost of a small lookup on each call.
- Dotted
import a.balso makesaitself available, plus anythingaimports in its__init__.py. __name__ == "__main__"is the usual guard for code that should run only when the file is executed directly.