The import statement imports Python modules, which is equivalent to calling the __import__() function. There is also the importlib.import_module() function which functions a differently.

Package vs Module

A Package is a collection of Modules. All packages are modules but not vice versa. A package can contain submodules or subpackages

There are 2 types of packages

  • Regular packages
  • Namespace packages

Regular Package

In a Regular Package, a directory has a __init__.py file, which is executed when the package is imported. The variables defined in the file are attached to the package’s namespace. The statement import a.b.c will

  • Import a : run a/__init__.py
  • Import a.b: run a/b/__init__.py
  • Import a.b.c: run a/b/c/__init__.py Any directory not being found will result in a ModuleNotFound exception

Importing a.b.c will import a, a.b and a.b.c. After that, importing a.b.cc will just import a.b.cc. The named modules are cached in sys.modules.

The module loading roughly happens like this:

  • If found in sys.modules then fine, return that
  • Else, add the module entry in sys.modules and start loading it. This is done to prevent infinite recursion when a module imports itself (or something from itself)
  • If the loading fails, only the failed module is removed from sys.modules. Any modules loaded as a side effect are kept.

Namespace Package

It has no __init__.py file. It is mainly used when files of a package are separated across different directories. They are present in Python 3.3+. Refer to PEP 420 for details.

Submodules

Consider the following directory structure:

a/
	__init__.py
	b.py

And __init__.py has the line from b import B. Also suppose we do import a. Then a.B will be the class B, and a.bwill refer to the module b.

Info

Importing a module only runs the __init__.py and associates global names there with the module. If __init__.py imports a submodule, that will be imported, otherwise not.