π Python Packages β Organizing Multiple Modules into a Structured Directory
Introduction π
A package in Python is a directory (folder) that contains multiple modules and an__init__.py file. Packages help organize large programs into clean, manageable, and reusable structures.
Note
π‘ A package = folder + modules (.py files) + __init__.py file
1. Why Packages? π§
- β Organize large codebases
- β Avoid naming conflicts
- β Reuse modules across projects
- β Create library-like structures
2. Creating Your First Package π§±
Step 1: Create a folder structure
package_structure
mypackage/
__init__.py
mathops.py
stringops.pyStep 2: Add code to modules
mathops.py
def add(a, b):
return a + b
def mul(a, b):
return a * bstringops.py
def uppercase(s):
return s.upper()
def reverse(s):
return s[::-1]Step 3: Import modules from the package
import_example.py
from mypackage.mathops import add
from mypackage.stringops import reverse
print(add(10, 20))
print(reverse("python"))3. The Role of __init__.py π§©
The __init__.py file tells Python that this folder should be treated as a package. It can be empty or used to control package imports.
__init__.py
from .mathops import add, mul
from .stringops import uppercase, reverseβ Now we can import directly:
direct_import.py
from mypackage import add, uppercase
print(add(2, 3))
print(uppercase("hello"))4. Nested Packages (Subpackages) π
nested_package_structure
mypackage/
__init__.py
math/
__init__.py
algebra.py
geometry.py
strings/
__init__.py
formats.pyUsing subpackages
nested_import.py
from mypackage.math.algebra import solve
from mypackage.strings.formats import titlecaseNote
β Subpackages help structure large applications even better.
5. Absolute vs Relative Imports π―
Absolute import
absolute_import.py
from mypackage.mathops import addRelative import (inside package)
relative_import.py
from .mathops import add
from .stringops import uppercaseNote
β Use relative imports inside packages for cleaner structure.
6. Installing External Packages π¦ (PIP)
pip_install.sh
pip install requests
pip install numpyβ These install third-party packages from the Python Package Index (PyPI).
7. Creating a Custom Package for Distribution π
Project Structure
dist_package_structure
mypackage/
mypackage/
__init__.py
tools.py
setup.py
README.mdsetup.py (Basic Example)
setup.py
from setuptools import setup, find_packages
setup(
name="mypackage",
version="1.0.0",
packages=find_packages(),
)8. Using __all__ to Control Imports πͺ
init_with_all.py
# inside __init__.py
__all__ = ["add", "uppercase"]
from .mathops import add
from .stringops import uppercaseβ Now only these functions are exposed when using:
import_all.py
from mypackage import *9. Real-World Project Example π
Folder Structure
realworld_package
ecommerce/
__init__.py
products.py
users.py
payments/
__init__.py
card.py
upi.pyUsage
realworld_usage.py
from ecommerce.products import get_products
from ecommerce.payments.card import process_card
get_products()
process_card(1000)10. When to Use Packages? π―
- β When your project grows large
- β When you want reusable modules
- β When separating concerns (auth, db, utils, UI)
- β When building distributable libraries
Conclusion π
>>βPackages turn Python files into organized, scalable ecosystems β perfect for real-world applications.β β¨
You now fully understand Packages in Python! Want the next topic? Try OOP (Classes & Objects), File Handling, Import System, or Virtual Environments. Just tell me! π