Introduction
Python’s ecosystem has over 500,000 packages on PyPI. This guide covers the most useful ones across categories — from data handling and HTTP to CLI tools and testing — that consistently appear in real-world projects.
Data & Files
xlsxwriter
Write Excel .xlsx files programmatically — create spreadsheets with formatting, charts, and formulas:
pip install xlsxwriter
import xlsxwriter
workbook = xlsxwriter.Workbook('report.xlsx')
worksheet = workbook.add_worksheet('Sales')
# Add formatting
bold = workbook.add_format({'bold': True, 'bg_color': '#4472C4', 'font_color': 'white'})
money = workbook.add_format({'num_format': '$#,##0.00'})
# Write headers
headers = ['Product', 'Q1', 'Q2', 'Q3', 'Q4', 'Total']
for col, header in enumerate(headers):
worksheet.write(0, col, header, bold)
# Write data
data = [
['Widget A', 1200, 1500, 1800, 2100],
['Widget B', 800, 950, 1100, 1300],
]
for row_num, row_data in enumerate(data, start=1):
for col_num, value in enumerate(row_data):
fmt = money if col_num > 0 else None
worksheet.write(row_num, col_num, value, fmt)
# Write SUM formula
worksheet.write_formula(row_num, 5, f'=SUM(B{row_num+1}:E{row_num+1})', money)
workbook.close()
print("report.xlsx created")
openpyxl
Read and write Excel files — also supports reading existing .xlsx files (xlsxwriter is write-only):
pip install openpyxl
from openpyxl import load_workbook
wb = load_workbook('data.xlsx')
ws = wb.active
for row in ws.iter_rows(min_row=2, values_only=True):
print(row)
pandas
The go-to library for data manipulation and analysis:
pip install pandas
import pandas as pd
# Read CSV
df = pd.read_csv('sales.csv')
# Basic analysis
print(df.describe())
print(df.groupby('region')['revenue'].sum())
# Filter and transform
high_value = df[df['revenue'] > 10000].copy()
high_value['margin'] = high_value['revenue'] - high_value['cost']
# Export
high_value.to_excel('high_value_sales.xlsx', index=False)
HTTP & APIs
requests
The standard HTTP library for Python — simple, elegant, and widely used:
pip install requests
import requests
# GET request
response = requests.get('https://api.example.com/users')
users = response.json()
# POST with JSON
response = requests.post(
'https://api.example.com/users',
json={'name': 'Alice', 'email': '[email protected]'},
headers={'Authorization': 'Bearer mytoken'}
)
# Session for persistent connections
session = requests.Session()
session.headers.update({'Authorization': 'Bearer mytoken'})
response = session.get('https://api.example.com/data')
httpx
Modern async-capable HTTP client — drop-in replacement for requests with async support:
pip install httpx
import httpx
import asyncio
# Sync (same as requests)
with httpx.Client() as client:
response = client.get('https://api.example.com/data')
# Async
async def fetch_all(urls):
async with httpx.AsyncClient() as client:
tasks = [client.get(url) for url in urls]
responses = await asyncio.gather(*tasks)
return [r.json() for r in responses]
Database
pymongo
Official MongoDB driver for Python:
pip install pymongo
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')
db = client['myapp']
users = db['users']
# Insert
users.insert_one({'name': 'Alice', 'email': '[email protected]', 'active': True})
# Query
active_users = list(users.find({'active': True}, {'_id': 0, 'name': 1, 'email': 1}))
# Update
users.update_one({'email': '[email protected]'}, {'$set': {'role': 'admin'}})
# Delete
users.delete_one({'email': '[email protected]'})
SQLAlchemy
The most popular Python ORM — works with PostgreSQL, MySQL, SQLite, and more:
pip install sqlalchemy
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import DeclarativeBase, Session
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String(100), nullable=False)
email = Column(String(200), unique=True)
engine = create_engine('sqlite:///app.db')
Base.metadata.create_all(engine)
with Session(engine) as session:
session.add(User(name='Alice', email='[email protected]'))
session.commit()
users = session.query(User).filter(User.name == 'Alice').all()
CLI & Scripting
click
Build beautiful CLI applications with decorators:
pip install click
import click
@click.group()
def cli():
"""My CLI tool."""
pass
@cli.command()
@click.argument('name')
@click.option('--count', default=1, help='Number of greetings')
@click.option('--uppercase', is_flag=True, help='Uppercase output')
def greet(name, count, uppercase):
"""Greet someone."""
for _ in range(count):
msg = f"Hello, {name}!"
click.echo(msg.upper() if uppercase else msg)
if __name__ == '__main__':
cli()
python app.py greet Alice --count 3 --uppercase
# => HELLO, ALICE!
# => HELLO, ALICE!
# => HELLO, ALICE!
rich
Beautiful terminal output — tables, progress bars, syntax highlighting, markdown:
pip install rich
from rich.console import Console
from rich.table import Table
from rich.progress import track
console = Console()
# Styled output
console.print("[bold green]✓ Success[/bold green]")
console.print("[bold red]✗ Error:[/bold red] something went wrong")
# Table
table = Table(title="Users")
table.add_column("Name", style="cyan")
table.add_column("Email", style="magenta")
table.add_column("Role", style="green")
table.add_row("Alice", "[email protected]", "Admin")
table.add_row("Bob", "[email protected]", "User")
console.print(table)
# Progress bar
for item in track(range(100), description="Processing..."):
pass # do work
python-dotenv
Load environment variables from a .env file:
pip install python-dotenv
from dotenv import load_dotenv
import os
load_dotenv() # loads .env file
DATABASE_URL = os.getenv('DATABASE_URL')
SECRET_KEY = os.getenv('SECRET_KEY')
DEBUG = os.getenv('DEBUG', 'false').lower() == 'true'
Validation & Serialization
pydantic
Data validation using Python type hints — widely used in FastAPI:
pip install pydantic
from pydantic import BaseModel, EmailStr, validator
from typing import Optional
class User(BaseModel):
name: str
email: EmailStr
age: Optional[int] = None
@validator('age')
def age_must_be_positive(cls, v):
if v is not None and v < 0:
raise ValueError('Age must be positive')
return v
# Validates automatically
user = User(name='Alice', email='[email protected]', age=30)
print(user.model_dump())
# Raises ValidationError on bad data
try:
User(name='Bob', email='not-an-email')
except Exception as e:
print(e)
Testing
pytest
The standard Python testing framework:
pip install pytest
# test_math.py
def add(a, b):
return a + b
def test_add():
assert add(2, 3) == 5
def test_add_negative():
assert add(-1, 1) == 0
def test_add_floats():
assert add(0.1, 0.2) == pytest.approx(0.3)
pytest test_math.py -v
faker
Generate realistic fake data for tests and development:
pip install faker
from faker import Faker
fake = Faker()
print(fake.name()) # => "John Smith"
print(fake.email()) # => "[email protected]"
print(fake.address()) # => "123 Main St, Springfield"
print(fake.phone_number()) # => "+1-555-123-4567"
print(fake.company()) # => "Acme Corp"
print(fake.text()) # => lorem ipsum paragraph
# Generate test dataset
users = [
{'name': fake.name(), 'email': fake.email(), 'city': fake.city()}
for _ in range(100)
]
Quick Reference
| Package | Category | Purpose |
|---|---|---|
xlsxwriter |
Files | Write Excel files |
openpyxl |
Files | Read/write Excel files |
pandas |
Data | Data manipulation and analysis |
requests |
HTTP | Simple HTTP client |
httpx |
HTTP | Async HTTP client |
pymongo |
Database | MongoDB driver |
sqlalchemy |
Database | ORM for SQL databases |
click |
CLI | CLI application framework |
rich |
CLI | Beautiful terminal output |
python-dotenv |
Config | Load .env files |
pydantic |
Validation | Data validation with type hints |
pytest |
Testing | Testing framework |
faker |
Testing | Fake data generation |
Resources
- PyPI — Python Package Index
- Python Packaging Guide
- Awesome Python — curated list of Python packages
Comments