Package in Python

A package is a folder/directory that contains modules.
• It usually contains a special file __init__.py.
• Packages are used to organize multiple modules under a common namespace.

Example Folder Structure:

project/
│
├── main.py
└── mypackage/
    ├── __init__.py
    ├── math_utils.py
    └── string_utils.py

mypackage/math_utils.py

Python

def multiply(a, b):
    return a * b

mypackage/string_utils.py

Python

def uppercase(text):
    return text.upper()

main.py

Python

from mypackage import math_utils, string_utils

print(math_utils.multiply(3, 4))       # Output: 12
print(string_utils.uppercase("hello"))  # Output: HELLO

✅ Here, mypackage is a package, containing modules math_utils and string_utils.

Using Modules Inside Packages

Python

from mypackage.math_utils import multiply
print(multiply(2, 5))  # Output: 10

Q: What is __init__.py? __init__.py is a special Python file that tells Python to treat a folder as a package. Without it, Python would see your folder as just a directory — not a package you can import from.

What You Can Put Inside __init__.py If we just want to mark a directory as a package, we can leave this file empty which is very common.

Python

# scripts/__init__.py
# empty file