Python Modules and Packages
Modules and packages help you organize Python code into reusable, maintainable units. A module is a single Python file, and a package is a directory of modules.
What is a Module?
A module is any Python file (.py) containing functions, classes, and variables:
python
# math_utils.py (this is a module)
PI = 3.14159
def add(a, b):
return a + b
def multiply(a, b):
return a * b
def circle_area(radius):
return PI * radius ** 2Importing Modules
Import the Entire Module
python
import math_utils
result = math_utils.add(3, 5)
area = math_utils.circle_area(5)
print(math_utils.PI)Import Specific Items
python
from math_utils import add, circle_area
result = add(3, 5) # No prefix needed
area = circle_area(5)Import with Alias
python
import math_utils as mu
result = mu.add(3, 5)
from math_utils import circle_area as ca
area = ca(5)Import Everything (Not Recommended)
python
from math_utils import * # Imports all public names
result = add(3, 5)
# Hard to track where functions come from - avoid this!Built-in Modules
Python comes with a rich standard library:
python
# Math operations
import math
print(math.sqrt(144)) # 12.0
print(math.factorial(5)) # 120
print(math.pi) # 3.141592653589793
# Date and time
from datetime import datetime, timedelta
now = datetime.now()
print(now.strftime("%Y-%m-%d %H:%M"))
future = now + timedelta(days=30)
# Random numbers
import random
print(random.randint(1, 100))
print(random.choice(["apple", "banana", "cherry"]))
print(random.shuffle([1, 2, 3, 4, 5]))
# Operating system
import os
print(os.getcwd()) # Current directory
print(os.listdir(".")) # List files
# JSON handling
import json
data = json.dumps({"name": "Alice"}, indent=2)
# Regular expressions
import re
match = re.search(r"\d+", "I have 42 apples")
print(match.group()) # 42
# Collections
from collections import Counter, defaultdict, namedtuple
counts = Counter("mississippi")
print(counts.most_common(3)) # [('s', 4), ('i', 4), ('p', 2)]Commonly Used Standard Library Modules
| Module | Purpose |
|---|---|
os | Operating system interface |
sys | System-specific functions |
math | Mathematical functions |
random | Random number generation |
datetime | Date and time handling |
json | JSON encoding/decoding |
csv | CSV file handling |
re | Regular expressions |
pathlib | Object-oriented file paths |
collections | Specialized containers |
itertools | Iterator building blocks |
functools | Higher-order functions |
typing | Type hints |
unittest | Testing framework |
logging | Logging facility |
Creating Packages
A package is a directory with an __init__.py file:
mypackage/
├── __init__.py
├── math_utils.py
├── string_utils.py
└── data/
├── __init__.py
└── loader.py__init__.py
python
# mypackage/__init__.py
"""MyPackage - A collection of utility functions."""
from .math_utils import add, multiply
from .string_utils import capitalize_words
__version__ = "1.0.0"
__all__ = ["add", "multiply", "capitalize_words"]Using the Package
python
# Import from package
from mypackage import add, multiply
from mypackage.string_utils import capitalize_words
from mypackage.data.loader import load_csv
# Or import the package
import mypackage
print(mypackage.__version__)The __name__ Variable
Every module has a __name__ attribute:
python
# utils.py
def helper():
return "I'm a helper"
# This code runs only when the file is executed directly
if __name__ == "__main__":
# Won't run when imported as a module
print("Running as main script")
print(helper())python
# main.py
import utils
print(utils.helper())
# "Running as main script" does NOT print hereThis pattern is essential for making modules both importable and directly executable.
The __all__ Variable
Controls what gets exported with from module import *:
python
# mymodule.py
__all__ = ["public_func", "PublicClass"]
def public_func():
return "I'm public"
def _private_func():
return "I'm private by convention"
def also_available():
return "I'm available via direct import but not with *"
class PublicClass:
passInstalling Third-Party Packages
bash
# Install with pip
pip install requests
pip install flask==3.0.0
pip install pandas numpy matplotlib
# Install from requirements file
pip install -r requirements.txt
# List installed packages
pip list
# Package info
pip show requests
# Uninstall
pip uninstall requestsUsing Third-Party Packages
python
# HTTP requests
import requests
response = requests.get("https://api.github.com")
data = response.json()
# Data analysis
import pandas as pd
df = pd.read_csv("data.csv")
print(df.head())
# Web framework
from flask import Flask
app = Flask(__name__)Module Search Path
Python searches for modules in this order:
- Current directory
PYTHONPATHenvironment variable- Standard library
site-packages(installed packages)
python
import sys
# View the search path
for path in sys.path:
print(path)
# Add a custom path
sys.path.append("/path/to/my/modules")Practical Example: Utility Package
python
# utils/__init__.py
"""Utility package for common operations."""
from .text import slugify, truncate
from .numbers import clamp, percentage
# utils/text.py
def slugify(text):
"""Convert text to URL-friendly slug."""
import re
text = text.lower().strip()
text = re.sub(r'[^\w\s-]', '', text)
text = re.sub(r'[\s_-]+', '-', text)
return text.strip('-')
def truncate(text, length=100, suffix="..."):
"""Truncate text to specified length."""
if len(text) <= length:
return text
return text[:length - len(suffix)].rsplit(' ', 1)[0] + suffix
# utils/numbers.py
def clamp(value, min_val, max_val):
"""Clamp a value between min and max."""
return max(min_val, min(value, max_val))
def percentage(part, whole):
"""Calculate percentage."""
if whole == 0:
return 0
return round((part / whole) * 100, 2)
# Usage
from utils import slugify, truncate, clamp, percentage
print(slugify("Hello World! This is Python"))
# hello-world-this-is-python
print(truncate("Python is a great language for beginners", 25))
# Python is a great...
print(clamp(150, 0, 100)) # 100
print(percentage(85, 100)) # 85.0Summary
- A module is a Python file; a package is a directory of modules with
__init__.py - Import styles:
import module,from module import func,import module as alias - Python's standard library provides 200+ modules for common tasks
- Use
__name__ == "__main__"to make modules both importable and executable - Use
__all__to control public API withfrom module import * - Install third-party packages with
pip install package_name - Python searches modules in: current dir → PYTHONPATH → stdlib → site-packages
Next, we'll learn about error handling in Python.