What is __init__.py for?
__init__.py
is a special Python file that is used to indicate that the directory it is present in is a Python package. It can contain initialization code for the package, or it can be an empty file.
Packages are a way to structure Python's module namespace by using "dotted module names". For example, the module name A.B
designates a submodule named B
in a package named A
.
You might have a directory structure like this:
mypkg/ __init__.py module1.py module2.py subpkg/ __init__.py submodule1.py submodule2.py
Here, mypkg
is a package that contains two modules: module1
and module2
. It also contains a subpackage, subpkg
, which itself contains two modules: submodule1
and submodule2
.
You can then import these modules and submodules using the dotted notation:
import mypkg.module1
import mypkg.subpkg.submodule1
The __init__.py
file can contain code to initialize the package when it is imported, for example by loading data or setting up logging. It can also be left empty.
# __init__.py
import logging
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
log.info('Initializing mypkg')
This __init__.py
file will run when you import mypkg
, setting up the package's logger.
import mypkg
# This will log 'Initializing mypkg'