Skip to main content
  1. Posts/

What is __init__.py?

··235 words·2 mins·

🐍 What is __init__.py and why is it important in Python?

If you work with Python, you’ve probably seen the mysterious __init__.py file in many folders… but do you really know what it’s for?

Real Python shares an article that explains how this file plays a key role in package organization:

🔹 1. Turns folders into Python packages

Before Python 3.3, every package had to include an __init__.py. Today it isn’t always required, but it’s still good practice to make it clear that a folder is an importable package.

🔹 2. Allows control over what gets imported

Inside __init__.py you can define:

__all__ = ["module1", "module2"]

This lets you decide which modules are exposed when using from package import *.

🔹 3. Facilitates package initialization

  • You can include configuration code that runs when the package is imported.
  • For example, automatically loading submodules or initializing global variables.

🔹 4. Improves project organization

Having well-configured __init__.py files makes your project:

  • Cleaner and easier to maintain
  • More predictable for other developers
  • Compatible with packaging and distribution tools

In short, __init__.py is the heart of a Python package:

It helps you structure, control, and maintain your project professionally.

More information at the link 👇

Also published on LinkedIn.
Juan Pedro Bretti Mandarano
Author
Juan Pedro Bretti Mandarano