Hackforge Academy

Category: python

what is __init__.py

Published on 02 Mar 2026

Explanation

__init__.py is a special file in Python that marks a directory as a package. It runs automatically when the package is imported. It can be empty or contain initialization logic.

Code:

# Directory structure
# mypackage/
# ├── __init__.py
# ├── module1.py
# └── module2.py

Explanation

You can import modules or functions inside __init__.py to control what is accessible when importing the package. This simplifies imports for users.

Code:

# __init__.py
from .module1 import greet

# Usage:
# from mypackage import greet

Explanation

Variables defined inside __init__.py become package-level attributes. This is useful for versioning and configuration settings.

Code:

# __init__.py
version = '1.0.0'

# Usage:
# import mypackage
# print(mypackage.version)

🚀 Learn Spring Boot with real-world projects

💡 Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
⚛️

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community