What is __init__.py used for?


The __init__.py file is used to mark a directory as a Python package. This file is usually empty, but can be used to initialize the package or set certain variables.

For example, suppose you have a directory called my_package that contains several Python modules. To make this directory into a package, you would create an __init__.py file in the my_package directory. This would allow you to use the import statement to import modules from the my_package directory, like this:

import my_package.my_module

In this case, my_package is the package, and my_module is a module in the package. The __init__.py file allows you to organize your Python code into reusable packages and modules.

Recommended Course

Learn Flask development and learn to build cool apps with our premium Python course on Udemy.