What is the __init__.py file in Python?
19 August 2022 (Updated 19 August 2022)
On this page
In a nutshell
The __init__.py
file tells Python to treat a directory as a package.
The__init__.py
file will automatically be executed any time the package is imported. This can be useful if you need to run some package initialization code or if you want to export some members (e.g., functions, classes, constants, etc) directly under the package namespace (e.g., mypackage.foo
) instead of under a module within the package (e.g., mypackage.mymodule.foo
).
You typically create an __init__.py
file for each directory that you want to act as a package (a collection of related modules). For example, the Django framework organizes its code into a number of packages, including the following:
Tagged:
Python
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment