Package Structure
2 min read ·
A package in Python is simply a folder that contains multiple Python modules. This helps in organizing related code into a structured format.
Example Package Structure
my_package/
__init__.py
module1.py
module2.py
Explanation of Each File
init.py
This file is used to indicate that the folder should be treated as a Python package.
- It can be empty
- It can also contain initialization code
- It allows Python to recognize the directory as a package
module1.py and module2.py
These are individual Python modules inside the package.
- Each file contains Python code
- They can have functions, classes, and variables
- They are used to divide functionality into smaller parts
How It Works
- The folder
my_packageacts as a container - Each
.pyfile inside it acts as a separate module - You can import and use these modules in other Python files
Note
Using packages helps in organizing large projects and avoiding confusion when working with multiple modules.
Basic Usage Example
Key Understanding
- Package → Folder
- Module → Python file
- Package contains multiple modules
- Used to structure and manage large codebases
Exercise
Create a folder named
my_package:- Add init.py file
- Create two modules with simple functions
Import them in another file and use their functions.