Zum Inhalt springen

Awesome-Python Libraries

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

FrameworkCommandPurpose
Djangopip install djangoFull-featured web framework
Flaskpip install flaskLightweight web framework
FastAPIpip install fastapi uvicornModern async framework
Pyramidpip install pyramidFlexible 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

PackageCommandPurpose
NumPypip install numpyNumerical computing
Pandaspip install pandasData manipulation
Matplotlibpip install matplotlibData visualization
Seabornpip install seabornStatistical visualization
Scikit-learnpip install scikit-learnMachine 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

FrameworkCommandPurpose
pytestpip install pytestTesting framework
unittestBuilt-inStandard testing
mockpip install mockMocking framework
coveragepip install coverageCode 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

CLI Tools

PackageCommandPurpose
Clickpip install clickCLI framework
Typerpip install typerModern CLI builder
argparseBuilt-inCommand-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

PackageCommandPurpose
loggingBuilt-inStandard logging
logurupip install loguruEnhanced logging
structlogpip install structlogStructured 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

PackageCommandPurpose
SQLAlchemypip install sqlalchemyORM framework
peeweepip install peeweeLightweight ORM
pymongopip install pymongoMongoDB driver
psycopg2pip install psycopg2PostgreSQL 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

PackageCommandPurpose
requestspip install requestsHTTP library
httpxpip install httpxAsync HTTP client
aiohttppip install aiohttpAsync 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

PackageCommandPurpose
asyncioBuilt-inAsync framework
aiofilespip install aiofilesAsync file operations
triopip install trioAsync 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

PackageCommandPurpose
python-dotenvpip install python-dotenvLoad .env files
pydanticpip install pydanticData validation
configparserBuilt-inConfig 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

PackageCommandPurpose
bcryptpip install bcryptPassword hashing
cryptographypip install cryptographyEncryption library
pyjwtpip install pyjwtJWT 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