Python Packages

Learn how to create, organize, import, and use Python packages with practical real-time examples from banking, e-commerce, employee management, billing, and application development.

What is a Package in Python?

A package is a way of organizing related Python modules into a directory structure. Packages help developers divide large applications into smaller, reusable, and maintainable components.

A package can contain multiple modules and subpackages. Traditionally, an __init__.py file is used to identify a directory as a Python package. Modern Python also supports namespace packages that do not require it.

Package

A directory that organizes related Python modules and subpackages.

Module

A Python file containing reusable functions, classes, and variables.

__init__.py

A package initialization file commonly used to define package-level behavior or exports.

Subpackage

A package inside another package, useful for organizing larger applications.

1. Basic Package Structure

Suppose we want to create a package called calculator.

project/
│
├── main.py
│
└── calculator/
    ├── __init__.py
    ├── addition.py
    └── subtraction.py

Here, calculator is the package and addition.py and subtraction.py are modules inside it.

2. Creating a Package

Create a folder named calculator and add an __init__.py file.

calculator/__init__.py
# Calculator package

print("Calculator package loaded")

The package can now contain additional modules such as addition.py and subtraction.py.

3. Module Inside a Package

Create an addition module inside the calculator package.

calculator/addition.py
def add(a, b):
    return a + b

Create another module for subtraction.

calculator/subtraction.py
def subtract(a, b):
    return a - b
4. Importing a Module from a Package
Package
→
Module
→
Import
→
Use Function
main.py
from calculator import addition

result = addition.add(20, 30)

print("Result:", result)
Output:
Result: 50
5. Importing a Specific Function

A specific function can be imported directly from a module inside a package.

main.py
from calculator.addition import add

result = add(100, 200)

print("Result:", result)
Output:
Result: 300
6. Importing Multiple Modules
main.py
from calculator import addition
from calculator import subtraction

print(addition.add(20, 10))
print(subtraction.subtract(20, 10))
Output:
30
10
7. Using __init__.py

The __init__.py file can be used to expose selected functions at the package level.

calculator/__init__.py
from .addition import add
from .subtraction import subtract

Now the functions can be imported directly from the package.

main.py
from calculator import add, subtract

print(add(50, 20))
print(subtract(50, 20))
Output:
70
30
8. Real-Time Example: Banking Package

A banking application can be divided into separate modules such as accounts, transactions, and loans.

banking/
│
├── __init__.py
├── account.py
├── transaction.py
└── loan.py
banking/account.py
class BankAccount:

    def __init__(self, account_number, balance):
        self.account_number = account_number
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount
        return self.balance

    def get_balance(self):
        return self.balance
main.py
from banking.account import BankAccount

account = BankAccount(
    "ACC1001",
    10000
)

account.deposit(5000)

print("Account:", account.account_number)
print("Balance:", account.get_balance())
Output:
Account: ACC1001
Balance: 15000
9. Real-Time Example: E-Commerce Package

An e-commerce application can organize product, order, payment, and customer functionality into packages and modules.

ecommerce/
│
├── __init__.py
│
├── products/
│   ├── __init__.py
│   └── product.py
│
├── orders/
│   ├── __init__.py
│   └── order.py
│
└── payments/
    ├── __init__.py
    └── payment.py
ecommerce/products/product.py
class Product:

    def __init__(self, name, price):
        self.name = name
        self.price = price

    def display(self):
        print(self.name)
        print(self.price)
main.py
from ecommerce.products.product import Product

product = Product(
    "Laptop",
    75000
)

product.display()
Output:
Laptop
75000
10. Real-Time Example: Employee Management Package

An HR system can separate employee, salary, attendance, and leave functionality.

hr/
│
├── __init__.py
├── employee.py
├── salary.py
├── attendance.py
└── leave.py
hr/employee.py
class Employee:

    def __init__(self, name, employee_id):
        self.name = name
        self.employee_id = employee_id

    def display(self):
        print("Employee:", self.name)
        print("ID:", self.employee_id)
hr/salary.py
def calculate_annual_salary(monthly_salary):
    return monthly_salary * 12


def calculate_bonus(monthly_salary):
    return monthly_salary * 0.10
main.py
from hr.employee import Employee
from hr.salary import calculate_bonus

employee = Employee(
    "Priya",
    "EMP101"
)

employee.display()

bonus = calculate_bonus(60000)

print("Bonus:", bonus)
Output:
Employee: Priya
ID: EMP101
Bonus: 6000.0
11. Real-Time Example: Payment Package

Payment applications can separate different payment methods into individual modules.

payment/
│
├── __init__.py
├── card.py
├── upi.py
└── netbanking.py
payment/card.py
def pay_by_card(amount):
    print("Processing card payment...")
    print("Amount:", amount)
    return "Card payment successful"
payment/upi.py
def pay_by_upi(amount):
    print("Processing UPI payment...")
    print("Amount:", amount)
    return "UPI payment successful"
main.py
from payment.card import pay_by_card

message = pay_by_card(2500)

print(message)
Output:
Processing card payment...
Amount: 2500
Card payment successful
12. Nested Packages

A package can contain another package. This is called a nested package or subpackage structure.

company/
│
├── __init__.py
│
├── hr/
│   ├── __init__.py
│   ├── employee.py
│   └── salary.py
│
└── finance/
    ├── __init__.py
    ├── invoice.py
    └── payment.py
main.py
from company.hr.employee import Employee
from company.finance.invoice import create_invoice

employee = Employee(
    "Rahul",
    "EMP500"
)

employee.display()

create_invoice(5000)

Nested packages are especially useful for large applications with many functional areas.

13. Package with Multiple Modules

One package can contain many modules, each responsible for a specific part of the application.

shopping/
│
├── __init__.py
├── product.py
├── cart.py
├── order.py
├── customer.py
└── payment.py

This structure makes it easier to maintain and test each component independently.

14. Importing with an Alias

Package modules can also be imported using aliases.

Python
import calculator.addition as add_module

result = add_module.add(25, 15)

print("Result:", result)
Output:
Result: 40
15. Package-Level Variables

Package-level information can be defined in __init__.py.

company/__init__.py
company_name = "ABC Technologies"
version = "1.0"
main.py
import company

print("Company:", company.company_name)
print("Version:", company.version)
Output:
Company: ABC Technologies
Version: 1.0
16. Real-Time Example: School Management Package

A school application can separate students, teachers, courses, and fees into different modules.

school/
│
├── __init__.py
├── student.py
├── teacher.py
├── course.py
└── fees.py
school/student.py
class Student:

    def __init__(self, name, roll_no):
        self.name = name
        self.roll_no = roll_no

    def display(self):
        print("Student:", self.name)
        print("Roll No:", self.roll_no)
main.py
from school.student import Student

student = Student(
    "Anita",
    101
)

student.display()
Output:
Student: Anita
Roll No: 101
17. Installing Third-Party Packages with pip

Python developers can install third-party packages using the pip package manager.

Command Prompt / Terminal
pip install requests

After installation, the package can be imported into a Python program.

Python
import requests

response = requests.get(
    "https://example.com"
)

print(response.status_code)
Note: Third-party packages are installed separately from Python's standard library.
18. Package vs Module
Feature Module Package
Meaning Python file containing code Organized collection of modules
Typical Extension .py Directory
Contains Functions, classes, variables Modules and subpackages
Example employee.py hr/
Purpose Reuse code Organize larger codebases
19. Package vs Module vs Library
Concept Meaning Example
Module A single Python file calculator.py
Package A structured collection of modules ecommerce/
Library Reusable collection of code distributed for use Requests
20. Real-Time Example: Complete Banking Application

A larger banking application can use several packages organized by responsibility.

banking_app/
│
├── main.py
│
├── accounts/
│   ├── __init__.py
│   ├── savings.py
│   └── current.py
│
├── transactions/
│   ├── __init__.py
│   ├── deposit.py
│   └── withdrawal.py
│
└── customers/
    ├── __init__.py
    └── customer.py
accounts/savings.py
class SavingsAccount:

    def __init__(self, account_number, balance):
        self.account_number = account_number
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

    def display(self):
        print("Account:", self.account_number)
        print("Balance:", self.balance)
main.py
from accounts.savings import SavingsAccount

account = SavingsAccount(
    "SAV1001",
    25000
)

account.deposit(5000)

account.display()
Output:
Account: SAV1001
Balance: 30000
21. Package Best Practices
  • Group related modules inside the same package.
  • Give packages meaningful names.
  • Keep package responsibilities clear.
  • Use subpackages for large applications.
  • Keep __init__.py simple when possible.
  • Expose only the functionality that should be part of the package API.
  • Avoid circular imports between modules and packages.
  • Use consistent naming conventions.
  • Document reusable packages.
  • Use virtual environments when managing third-party dependencies.
Remember: A good package structure makes a large application easier to understand, test, maintain, and extend.
22. Complete Real-Time Example: E-Commerce Application

The following structure demonstrates how a real e-commerce application can use packages to separate different responsibilities.

ecommerce_app/
│
├── main.py
│
├── products/
│   ├── __init__.py
│   └── product.py
│
├── customers/
│   ├── __init__.py
│   └── customer.py
│
├── orders/
│   ├── __init__.py
│   └── order.py
│
└── payments/
    ├── __init__.py
    └── payment.py
products/product.py
class Product:

    def __init__(self, name, price):
        self.name = name
        self.price = price
payments/payment.py
def process_payment(amount):
    print("Processing payment:", amount)
    return True
main.py
from products.product import Product
from payments.payment import process_payment

product = Product(
    "Laptop",
    75000
)

print("Product:", product.name)
print("Price:", product.price)

if process_payment(product.price):
    print("Order completed successfully")
Output:
Product: Laptop
Price: 75000
Processing payment: 75000
Order completed successfully
23. Package Structure Flow
Package
→
Modules
→
Import
→
Application
  1. Create a package directory.
  2. Add related modules to the package.
  3. Use __init__.py when appropriate.
  4. Import required modules or functions.
  5. Use the package functionality in the application.
24. Important Python Package Rules
Rule Explanation
Package Organizes related Python modules.
Module A Python file inside or outside a package.
__init__.py Can initialize a package and expose package-level names.
Subpackage A package contained inside another package.
from package.module import Imports selected functionality.
pip Installs packages distributed through Python's package ecosystem.
Virtual Environment Provides an isolated environment for project dependencies.
Key Idea: Modules help organize reusable code, while packages provide a higher-level structure for organizing multiple related modules and subpackages in larger Python applications.

Python Packages

Learn to organize Python modules into clean, reusable application structures.