What is __init__.py in a Python Package? – TecAdmin

Channel: Linux
Abstract: If we want to expose only the tiger and sparrow functions to someone importing the animals packagea package is simply a way of organizing related modu

Python is a versatile language that provides a rich ecosystem for developers. One of its features is the ability to organize code using modules and packages. In the context of Python packages, you may have come across a file named __init__.py. If you’ve wondered about its purpose and usage, this article aims to shed some light.


In Python, a package is simply a way of organizing related modules into a single directory hierarchy. The __init__.py file serves as the initializer for the package, and it’s presence signals to Python that a directory should be treated as a package or a sub-package.

Main Purposes of __init__.py:
  • Directory as a Package: As mentioned earlier, the mere presence of __init__.py lets Python recognize a directory as a package. Without it, even if the directory contains multiple Python modules, it won’t be recognized as a package.
  • Initialization: When you import a package, the __init__.py file is the first to be executed. So, you can use it to execute any initialization code that a package may need.
  • Managing Imports: It can be used to facilitate easier imports. For instance, if a package has multiple modules, you can use the __init__.py file to expose selected functions, classes, or variables to make importing more convenient for the user.

Let’s say we have a package named animals and inside that package, we have two modules: mammals.py and birds.py.

Directory structure: Here is the sample directory structure for the animals package:

  |-- __init__.py
  |-- mammals.py
  |-- birds.py


def tiger():
    return "This is a mammal."

def human():
    return "Humans are also mammals."


def sparrow():
    return "This is a bird."

def parrot():
    return "Parrots are colorful birds."

If we want to expose only the tiger and sparrow functions to someone importing the animals package, we can modify the __init__.py file as follows:


from .mammals import tiger
from .birds import sparrow

Now, someone using the package can easily do:

from animals import tiger, sparrow

print(tiger())  # Output: This is a mammal.
print(sparrow())  # Output: This is a bird.

Best Practices:
  • Keep it Lightweight: It’s a best practice to keep the __init__.py file lightweight. While you can put executable code in it, if the code is too heavy, it might slow down the import of your package, which could be detrimental to performance.
  • Clear Import Paths: Use the __init__.py file to facilitate clear and intuitive import paths for your package users.
  • Documentation: It’s always good to include a short comment or docstring explaining the package and any non-obvious imports or initializations in the __init__.py file.

The __init__.py file plays a crucial role in the Python package ecosystem. While it might seem unassuming, it has a multifaceted function in initializing, organizing, and managing the package’s structure. Properly using __init__.py can lead to cleaner, more understandable code and a better experience for those using your Python package.

Ref From: tecadmin

Related articles