Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is __init__.py in Python?
In Python, __init__.py is a special file that makes a directory a Python package. When Python encounters a directory during import, it looks for the __init__.py file to determine how to initialize the package and what code should be executed.
The __init__.py file serves two main purposes:
- It makes the directory a Python package so the interpreter can find modules inside
- It can contain package initialization code, such as importing submodules, defining variables, or executing setup code
Package Structure with __init__.py
Here's a typical package structure ?
mypackage/
__init__.py
module1.py
module2.py
subpackage/
__init__.py
module3.py
Creating a Simple Package
Let's create a practical example with an operations package containing mathematical functions ?
Step 1: Create the Package Structure
operations/ ??? __init__.py ??? add.py ??? subtract.py ??? multiply.py
Step 2: Create Module Files
add.py ?
def add(a, b):
"""Add two numbers"""
return a + b
subtract.py ?
def subtract(a, b):
"""Subtract two numbers"""
return a - b
multiply.py ?
def multiply(a, b):
"""Multiply two numbers"""
return a * b
Step 3: Configure __init__.py
The __init__.py file can contain initialization code ?
print("Operations package initialized!")
# Make functions available at package level
from .add import add
from .subtract import subtract
from .multiply import multiply
__version__ = "1.0.0"
__all__ = ['add', 'subtract', 'multiply']
Using the Package
Once created, you can import and use the package in different ways ?
Method 1: Import Specific Functions
from operations import add, multiply
result1 = add(10, 5)
result2 = multiply(3, 4)
print(f"Addition: {result1}")
print(f"Multiplication: {result2}")
Operations package initialized! Addition: 15 Multiplication: 12
Method 2: Import Entire Package
import operations
result = operations.subtract(20, 8)
print(f"Subtraction: {result}")
print(f"Package version: {operations.__version__}")
Operations package initialized! Subtraction: 12 Package version: 1.0.0
Method 3: Import with Alias
import operations as ops
result = ops.add(7, 3)
print(f"Result using alias: {result}")
Operations package initialized! Result using alias: 10
Empty __init__.py Files
An __init__.py file can be completely empty. Even empty, it still marks the directory as a package ?
# Empty __init__.py file # This directory is still a valid Python package
Key Points
- __init__.py is executed when the package is first imported
- It can be empty or contain initialization code
- Use
__all__to control what gets imported withfrom package import * - Since Python 3.3, __init__.py is not strictly required for namespace packages
Conclusion
The __init__.py file is essential for creating Python packages. It transforms directories into importable packages and allows you to control package initialization behavior and expose specific functionality to users.
---