A comprehensive guide to essential Python packages organized by category with pip commands and usage examples.
Package Management
# Install package
pip install package-name
# Install specific version
pip install package-name==1.2.3
# Install from requirements file
pip install -r requirements.txt
# Create requirements file
pip freeze > requirements.txt
# Create virtual environment
python -m venv venv
source venv/bin/activate # Linux/macOS
venv\Scripts\activate # Windows
# Update pip
pip install --upgrade pip
Web Frameworks
| Framework | Command | Purpose |
|---|
| Django | pip install django | Full-featured web framework |
| Flask | pip install flask | Lightweight web framework |
| FastAPI | pip install fastapi uvicorn | Modern async framework |
| Pyramid | pip install pyramid | Flexible web framework |
Flask REST API Example
pip install flask flask-cors
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route('/api/users', methods=['GET'])
def get_users():
return jsonify([{'id': 1, 'name': 'John'}])
@app.route('/api/users', methods=['POST'])
def create_user():
data = request.json
return jsonify(data), 201
if __name__ == '__main__':
app.run(debug=True)
FastAPI Example
pip install fastapi uvicorn sqlalchemy
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class User(BaseModel):
id: int
name: str
email: str
@app.get("/users/{user_id}")
async def get_user(user_id: int):
return {"id": user_id, "name": "John"}
@app.post("/users")
async def create_user(user: User):
return user
Data Science & Analysis
| Package | Command | Purpose |
|---|
| NumPy | pip install numpy | Numerical computing |
| Pandas | pip install pandas | Data manipulation |
| Matplotlib | pip install matplotlib | Data visualization |
| Seaborn | pip install seaborn | Statistical visualization |
| Scikit-learn | pip install scikit-learn | Machine learning |
Pandas Data Processing
pip install pandas numpy
import pandas as pd
import numpy as np
# Read CSV
df = pd.read_csv('data.csv')
# Data exploration
print(df.head())
print(df.info())
print(df.describe())
# Data filtering
filtered = df[df['age'] > 25]
# Grouping
grouped = df.groupby('category').sum()
# Data cleaning
df.dropna()
df.fillna(0)
# Merging
merged = pd.merge(df1, df2, on='id')
NumPy Arrays
import numpy as np
# Create arrays
arr = np.array([1, 2, 3, 4, 5])
matrix = np.array([[1, 2], [3, 4]])
# Array operations
arr + 10
arr * 2
arr[arr > 2]
# Mathematical functions
np.mean(arr)
np.std(arr)
np.sum(arr)
Testing Frameworks
| Framework | Command | Purpose |
|---|
| pytest | pip install pytest | Testing framework |
| unittest | Built-in | Standard testing |
| mock | pip install mock | Mocking framework |
| coverage | pip install coverage | Code coverage |
pytest Example
pip install pytest pytest-cov
# test_calculator.py
import pytest
def add(a, b):
return a + b
def test_addition():
assert add(2, 3) == 5
def test_negative_numbers():
assert add(-1, -1) == -2
@pytest.mark.parametrize("a,b,expected", [
(1, 1, 2),
(2, 3, 5),
(-1, 1, 0),
])
def test_add_multiple(a, b, expected):
assert add(a, b) == expected
Running Tests
# Run all tests
pytest
# Run with verbose output
pytest -v
# Run specific test
pytest test_calculator.py::test_addition
# Generate coverage report
pytest --cov=. --cov-report=html
| Package | Command | Purpose |
|---|
| Click | pip install click | CLI framework |
| Typer | pip install typer | Modern CLI builder |
| argparse | Built-in | Command-line parser |
Click CLI Example
pip install click
import click
@click.command()
@click.option('--name', prompt='Your name', help='Name to greet')
@click.option('--count', default=1, help='Number of greetings')
def hello(name, count):
for _ in range(count):
click.echo(f'Hello {name}!')
if __name__ == '__main__':
hello()
Logging
| Package | Command | Purpose |
|---|
| logging | Built-in | Standard logging |
| loguru | pip install loguru | Enhanced logging |
| structlog | pip install structlog | Structured logging |
Python Logging
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
logger.info('Application started')
logger.error('An error occurred')
Loguru Example
pip install loguru
from loguru import logger
logger.add("file.log", rotation="500 MB")
logger.info("Info message")
logger.error("Error message")
Database & ORM
| Package | Command | Purpose |
|---|
| SQLAlchemy | pip install sqlalchemy | ORM framework |
| peewee | pip install peewee | Lightweight ORM |
| pymongo | pip install pymongo | MongoDB driver |
| psycopg2 | pip install psycopg2 | PostgreSQL adapter |
SQLAlchemy ORM
pip install sqlalchemy
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
email = Column(String, unique=True)
engine = create_engine('postgresql://user:password@localhost/dbname')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
# Create
user = User(name='John', email='john@example.com')
session.add(user)
session.commit()
# Read
users = session.query(User).all()
# Update
user.name = 'Jane'
session.commit()
# Delete
session.delete(user)
session.commit()
HTTP Requests
| Package | Command | Purpose |
|---|
| requests | pip install requests | HTTP library |
| httpx | pip install httpx | Async HTTP client |
| aiohttp | pip install aiohttp | Async HTTP framework |
Requests Example
pip install requests
import requests
# GET request
response = requests.get('https://api.github.com/users/github')
print(response.json())
# POST request
data = {'name': 'John', 'email': 'john@example.com'}
response = requests.post('https://api.example.com/users', json=data)
# Custom headers
headers = {'Authorization': 'Bearer token'}
response = requests.get('https://api.example.com', headers=headers)
# Error handling
try:
response = requests.get('https://api.example.com')
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f'Error: {e}')
Async Programming
| Package | Command | Purpose |
|---|
| asyncio | Built-in | Async framework |
| aiofiles | pip install aiofiles | Async file operations |
| trio | pip install trio | Async library |
Asyncio Example
import asyncio
async def fetch_data(url):
await asyncio.sleep(1)
return f'Data from {url}'
async def main():
tasks = [
fetch_data('https://example.com'),
fetch_data('https://example.org'),
]
results = await asyncio.gather(*tasks)
print(results)
asyncio.run(main())
Environment & Configuration
| Package | Command | Purpose |
|---|
| python-dotenv | pip install python-dotenv | Load .env files |
| pydantic | pip install pydantic | Data validation |
| configparser | Built-in | Config file parsing |
python-dotenv Example
pip install python-dotenv
import os
from dotenv import load_dotenv
load_dotenv()
database_url = os.getenv('DATABASE_URL')
api_key = os.getenv('API_KEY')
Security
| Package | Command | Purpose |
|---|
| bcrypt | pip install bcrypt | Password hashing |
| cryptography | pip install cryptography | Encryption library |
| pyjwt | pip install pyjwt | JWT tokens |
Bcrypt Example
pip install bcrypt
import bcrypt
# Hash password
password = b'mypassword'
hashed = bcrypt.hashpw(password, bcrypt.gensalt())
# Verify password
if bcrypt.checkpw(password, hashed):
print('Password correct')
Common Commands
# Install dependencies from requirements.txt
pip install -r requirements.txt
# Generate requirements
pip freeze > requirements.txt
# Check outdated packages
pip list --outdated
# Update package
pip install --upgrade package-name
# Uninstall package
pip uninstall package-name
# Run Python script
python script.py
# Run Python interactive shell
python -i script.py
# Run with venv
source venv/bin/activate && python script.py
Project Structure
myproject/
├── venv/
├── src/
│ ├── __init__.py
│ ├── main.py
│ ├── config.py
│ └── models/
├── tests/
│ ├── __init__.py
│ └── test_main.py
├── requirements.txt
├── .env
└── README.md
Best Practices
- Use virtual environments for each project
- Pin exact versions in requirements.txt
- Use type hints for better code clarity
- Write docstrings for all functions
- Follow PEP 8 style guide
- Use pytest for testing
- Use logging instead of print()
- Handle exceptions properly
- Use context managers for resource management
- Keep functions small and focused
Last updated: 2026-03-30