Packages in Python
A package is a collection of related Python modules organized inside a directory.
Packages help us organize large projects into smaller and manageable parts.
1. Module vs Package
Module
A single Python file:
calculator.py
Package
A folder containing related Python modules:
mypackage/ │ ├── calculator.py ├── converter.py └── validator.py
So:
.py file → Module Folder → Package
2. Creating a Package
Suppose we create this structure:
project/ │ ├── main.py │ └── mypackage/ ├── __init__.py ├── calculator.py └── converter.py
Here, mypackage is our package.
3. __init__.py
Traditionally, a Python package contains a file named:
__init__.py
Example:
mypackage/ ├── __init__.py ├── calculator.py └── converter.py
It can be empty:
# __init__.py
It can also contain package-level code or imports.
Modern Python can also support namespace packages without __init__.py, but beginners will commonly see and use __init__.py.
4. Module Inside a Package
calculator.py
def add(a, b): return a + b def subtract(a, b): return a - b
Now this module is inside mypackage.
5. Importing a Module from a Package
In main.py:
from mypackage import calculator print(calculator.add(10, 20))
Output:
30
6. Importing a Function
You can directly import a function.
from mypackage.calculator import add print(add(10, 20))
Output:
30
7. Importing Multiple Functions
from mypackage.calculator import add, subtract print(add(10, 20)) print(subtract(20, 10))
Output:
30 10
8. Package with Multiple Modules
Suppose:
calculator.py
def add(a, b): return a + b
converter.py
def km_to_miles(km): return km * 0.621371
Then:
from mypackage.calculator import add from mypackage.converter import km_to_miles print(add(10, 20)) print(km_to_miles(10))
Output:
30 6.21371
9. Package Structure
A larger project might look like this:
myproject/ │ ├── main.py │ ├── users/ │ ├── __init__.py │ ├── login.py │ └── register.py │ ├── products/ │ ├── __init__.py │ ├── product.py │ └── price.py │ └── database/ ├── __init__.py └── connection.py
This makes a large application easier to organize.
10. Subpackages
A package can contain another package.
Example:
project/ │ ├── main.py │ └── ecommerce/ ├── __init__.py │ ├── users/ │ ├── __init__.py │ └── login.py │ └── products/ ├── __init__.py └── product.py
Here:
ecommerce → Package users → Subpackage products → Subpackage login.py → Module product.py → Module
11. Importing from a Subpackage
from ecommerce.users.login import login_user login_user()
The import path follows the folder structure.
ecommerce.users.login │ │ │ │ │ └── Module │ └─────── Subpackage └─────────────── Package
12. Using as
You can give an imported package or module an alias.
import mypackage.calculator as calc print(calc.add(10, 20))
Output:
30
13. Standard Library Packages
Python provides many useful modules and packages as part of its standard library.
Examples:
import os import json import pathlib import email import urllib
Some are modules, while others contain multiple modules/subpackages.
14. External Packages
Python has a huge ecosystem of packages created by developers.
For example:
requests→ HTTP requestsnumpy→ numerical computingpandas→ data analysisflask→ web developmentdjango→ web developmentopencv-python→ computer vision
External packages can commonly be installed using pip.
Example:
pip install requests
Then:
import requests
15. Package Manager — pip
pip is commonly used to install and manage Python packages.
Install
pip install requests
Upgrade
pip install --upgrade requests
Uninstall
pip uninstall requests
Show installed packages
pip list
Show package information
pip show requests
16. Requirements File
A project can store its required packages in:
requirements.txt
Example:
requests flask numpy pandas
Install all of them:
pip install -r requirements.txt
This is useful when sharing a project with other developers.
17. Package vs Module vs Library
These terms are often confused.
| Term | Meaning |
|---|---|
| Module | Usually one .py file |
| Package | Collection of related modules |
| Library | Collection of reusable code, often containing modules/packages |
pip | Tool used to install/manage Python packages |
Example:
mypackage/ │ ├── calculator.py ← Module ├── converter.py ← Module └── validator.py ← Module ↑ Package
18. Complete Example
Project structure
studentproject/ │ ├── main.py │ └── tools/ ├── __init__.py ├── calculator.py └── student.py
calculator.py
def add(a, b): return a + b
student.py
def get_student(): return "Ram"
main.py
from tools.calculator import add from tools.student import get_student print("Student:", get_student()) print("Result:", add(10, 20))
Output:
Student: Ram Result: 30
Package Structure
Python Package │ ├── Package │ │ │ ├── __init__.py │ │ │ ├── Module 1 │ │ └── calculator.py │ │ │ ├── Module 2 │ │ └── converter.py │ │ │ └── Module 3 │ └── validator.py │ └── Subpackages └── More modules
Key Points
- A module is generally a Python file.
- A package organizes related modules into directories.
__init__.pyis commonly used in traditional Python packages.- Use
importorfrom ... import ...to use package contents. pipis commonly used to install external packages.- Packages make large Python projects organized, reusable, and easier to maintain.