Python docs - The Import System
The Import system from the Python Docs#
Python code in one module gains access to code in another module by importing it
The import statement is the most common way but you can also:
importlib.import_module()__import__()
The import statement:
- it searches for the named module
- it binds the results of that search to a name in the local scope
A direct call to
__import__()performs only the module search and, if found, the module creation operation.
If the named module cannot be found, a ModuleNotFoundError is raised
importlib - is an interface for interacting with the import system
Packages#
Packages:
- organise modules
- provide a naming hierachy
You can think of packages as the directories on a file system and modules as files within directories
It’s important to keep in mind that all packages are modules, but not all modules are packages.
Any module that contains a __path__ attribute is considered a package
All modules have a name. Subpackage names are separated from their parent package name by a dot, akin to Python’s standard attribute access syntax. Eg.
email.mime.text
Regular packages#
- A regular package is typically implemented as a directory containing an
__init__.pyfile. - When a regular package is imported, this
__init__.pyfile is implicitly executed, and the objects it defines are bound to names in the package’s namespace. (A good place to setup logging) -
The
__init__.pyfile can contain the same Python code that any other module can containparent/ init.py one/ init.py two/ init.py three/ init.py
That is a top level parent with 3 subpackages. Importing
parent.oneimplicitly executesparent/__init__.pyandparent/one/__init__.py
Namespace packages#
- Namespace packages may or may not correspond directly to objects on the file system; they may be virtual modules that have no concrete representation.
- With namespace packages, there is no
parent/__init__.pyfile. In fact, there may be multiple parent directories found during import search, where each one is provided by a different portion.
Searching#
- Python needs the fully qualified name - dotted name showing the “path” from a module’s global scope to a class, function or method.
- e.g.
foo.bar.bazIn this case, Python first tries to importfoo, thenfoo.bar, and finallyfoo.bar.baz. If any of the intermediate imports fail, a ModuleNotFoundError is raised. - During import, the module name is looked up in
sys.modules - Python includes a number of default finders and importers. The first one knows how to locate built-in modules, and the second knows how to locate frozen modules. A third default finder searches an import path for modules. The import path is a list of locations that may name file system paths or zip files.
Keywords#
__path__- if a module has a__path__attribute, it is a package. Non-package modules should not have a__path__attribute.__name__- fully qualified name of the module - uniquely identifies the module__loader__- the loader object used to laod the module__package__- When the module is a package, its__package__value should be set to its__name__. When the module is not a package,__package__- should be set to the empty string for top-level modules, or for submodules, to the parent package’s name__spec__- the moduel specification used to import the module__file__- is optional. If set, this attribute’s value must be a string. Import system may leave it unset.