Python 的强大之处在于其丰富的标准库和庞大的第三方库生态系统。下面我将详细介绍 Python 的标准库和常用第三方库。

一、Python 标准库

1. 操作系统接口 (os, sys)

import os
import sys

# 操作系统接口示例
print("当前工作目录:", os.getcwd())
print("系统平台:", sys.platform)

# 文件和目录操作
if not os.path.exists("test_dir"):
    os.makedirs("test_dir")  # 创建目录

# 遍历目录
for file in os.listdir("."):
    if file.endswith(".py"):
        print("Python文件:", file)

# 环境变量
print("PATH环境变量:", os.getenv("PATH", "未设置"))

# 命令行参数
print("脚本名称:", sys.argv[0])
print("命令行参数:", sys.argv[1:])

# 退出程序
# sys.exit(0)  # 正常退出

2. 文件处理 (pathlib, shutil, glob)

from pathlib import Path
import shutil
import glob

# 现代文件路径处理 (Python 3.4+)
current_file = Path(__file__)
print("文件路径:", current_file)
print("父目录:", current_file.parent)
print("文件名:", current_file.name)
print("文件后缀:", current_file.suffix)

# 创建新路径
new_file = current_file.parent / "data" / "example.txt"
new_file.parent.mkdir(parents=True, exist_ok=True)  # 创建目录

# 文件操作
with open(new_file, "w") as f:
    f.write("Hello, Pathlib!")

# 文件信息
if new_file.exists():
    print("文件大小:", new_file.stat().st_size, "bytes")

# 复制、移动文件
shutil.copy(new_file, new_file.parent / "backup.txt")

# 模式匹配文件
py_files = glob.glob("*.py")
print("Python文件:", py_files)

3. 日期和时间处理 (datetime, time, calendar)

from datetime import datetime, date, time, timedelta
import time as time_module
import calendar

# 当前时间
now = datetime.now()
print("当前时间:", now)
print("格式化时间:", now.strftime("%Y-%m-%d %H:%M:%S"))

# 时间运算
tomorrow = now + timedelta(days=1)
last_week = now - timedelta(weeks=1)
print("明天:", tomorrow.date())
print("上周:", last_week.date())

# 时间戳
timestamp = time_module.time()
print("当前时间戳:", timestamp)
print("时间戳转时间:", datetime.fromtimestamp(timestamp))

# 解析字符串为时间
date_str = "2024-01-15 14:30:00"
parsed_date = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print("解析的时间:", parsed_date)

# 日历操作
cal = calendar.month(2024, 1)
print("2024年1月日历:")
print(cal)

4. 数据序列化 (json, pickle, csv)

import json
import pickle
import csv
from dataclasses import dataclass

# JSON 序列化 (适合跨语言数据交换)
data = {
    "name": "张三",
    "age": 30,
    "hobbies": ["阅读", "编程", "旅行"],
    "is_student": False
}

# 写入JSON文件
with open("data.json", "w", encoding="utf-8") as f:
    json.dump(data, f, ensure_ascii=False, indent=2)

# 读取JSON文件
with open("data.json", "r", encoding="utf-8") as f:
    loaded_data = json.load(f)
    print("JSON数据:", loaded_data)

# Pickle 序列化 (Python专用,支持复杂对象)
@dataclass
class Person:
    name: str
    age: int

person = Person("李四", 25)
with open("person.pkl", "wb") as f:  # 注意二进制模式
    pickle.dump(person, f)

with open("person.pkl", "rb") as f:
    loaded_person = pickle.load(f)
    print("Pickle数据:", loaded_person)

# CSV 文件处理
# 写入CSV
with open("data.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.writer(f)
    writer.writerow(["姓名", "年龄", "城市"])  # 表头
    writer.writerow(["王五", 28, "北京"])
    writer.writerow(["赵六", 35, "上海"])

# 读取CSV
with open("data.csv", "r", encoding="utf-8") as f:
    reader = csv.reader(f)
    print("CSV数据:")
    for row in reader:
        print(row)

5. 数学计算 (math, random, statistics)

import math
import random
import statistics

# 数学计算
print("圆周率:", math.pi)
print("平方根:", math.sqrt(16))
print("对数:", math.log(100, 10))
print("正弦值:", math.sin(math.pi/2))

# 随机数
print("随机整数:", random.randint(1, 100))
print("随机浮点数:", random.uniform(0, 1))
print("随机选择:", random.choice(["A", "B", "C"]))

# 打乱列表
items = [1, 2, 3, 4, 5]
random.shuffle(items)
print("打乱后的列表:", items)

# 统计计算
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("平均值:", statistics.mean(data))
print("中位数:", statistics.median(data))
print("标准差:", statistics.stdev(data))
print("众数:", statistics.mode([1, 1, 2, 3, 3, 3, 4]))

6. 网络编程 (urllib, socket, http)

import urllib.request
import urllib.parse
from http.server import HTTPServer, BaseHTTPRequestHandler
import socket

# 简单的HTTP请求
try:
    with urllib.request.urlopen("https://httpbin.org/json") as response:
        data = response.read().decode("utf-8")
        print("HTTP响应长度:", len(data))
except Exception as e:
    print("HTTP请求错误:", e)

# URL解析
url = "https://example.com/path?name=value&age=30"
parsed = urllib.parse.urlparse(url)
print("协议:", parsed.scheme)
print("域名:", parsed.netloc)
print("路径:", parsed.path)
print("查询参数:", parsed.query)

# 构建查询参数
params = urllib.parse.urlencode({"q": "python", "page": 1})
print("编码的参数:", params)

# 简单的HTTP服务器
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-Type", "text/plain; charset=utf-8")
        self.end_headers()
        self.wfile.write("Hello, HTTP服务器!".encode("utf-8"))

# 启动服务器(取消注释运行)
# httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler)
# print("服务器运行在 http://localhost:8000")
# httpd.serve_forever()

7. 并发编程 (threading, multiprocessing, asyncio)

import threading
import multiprocessing
import asyncio
import time
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor

# 多线程示例
def print_numbers(thread_name, count):
    for i in range(count):
        print(f"{thread_name}: {i}")
        time.sleep(0.1)

# 创建并启动线程
thread1 = threading.Thread(target=print_numbers, args=("线程1", 5))
thread2 = threading.Thread(target=print_numbers, args=("线程2", 5))

thread1.start()
thread2.start()
thread1.join()
thread2.join()

# 线程池
def square_number(n):
    return n * n

with ThreadPoolExecutor(max_workers=3) as executor:
    numbers = [1, 2, 3, 4, 5]
    results = executor.map(square_number, numbers)
    print("线程池结果:", list(results))

# 异步编程
async def async_task(name, seconds):
    print(f"开始异步任务: {name}")
    await asyncio.sleep(seconds)
    print(f"完成异步任务: {name}")
    return f"{name} 结果"

async def main_async():
    tasks = [
        async_task("任务1", 2),
        async_task("任务2", 1),
        async_task("任务3", 3)
    ]
    results = await asyncio.gather(*tasks)
    print("异步任务结果:", results)

# 运行异步函数
asyncio.run(main_async())

8. 数据压缩和归档 (zlib, gzip, tarfile, zipfile)

import zlib
import gzip
import tarfile
import zipfile

# 数据压缩
original_data = b"这是一段需要压缩的文本数据" * 100
compressed_data = zlib.compress(original_data)
decompressed_data = zlib.decompress(compressed_data)

print("原始数据大小:", len(original_data))
print("压缩后大小:", len(compressed_data))
print("压缩比:", len(compressed_data) / len(original_data))
print("数据一致性:", original_data == decompressed_data)

# GZIP 文件压缩
with gzip.open("example.txt.gz", "wb") as f:
    f.write(original_data)

with gzip.open("example.txt.gz", "rb") as f:
    content = f.read()
    print("GZIP解压数据长度:", len(content))

# ZIP 文件处理
with zipfile.ZipFile("example.zip", "w") as zf:
    zf.write("data.json")  # 添加文件到ZIP

with zipfile.ZipFile("example.zip", "r") as zf:
    print("ZIP文件内容:", zf.namelist())
    zf.extractall("extracted")  # 解压所有文件

# TAR 归档
with tarfile.open("example.tar.gz", "w:gz") as tf:
    tf.add("data.json")  # 添加文件到TAR

with tarfile.open("example.tar.gz", "r:gz") as tf:
    tf.extractall("tar_extracted")

二、重要第三方库

1. 数据科学库

# 安装命令: pip install numpy pandas matplotlib scikit-learn

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# NumPy - 数值计算
array = np.array([[1, 2, 3], [4, 5, 6]])
print("NumPy数组:")
print(array)
print("形状:", array.shape)
print("平均值:", np.mean(array))

# 数组运算
result = array * 2 + 1
print("数组运算结果:")
print(result)

# Pandas - 数据分析
data = {
    '姓名': ['张三', '李四', '王五', '赵六'],
    '年龄': [25, 30, 35, 28],
    '城市': ['北京', '上海', '广州', '深圳'],
    '工资': [50000, 60000, 70000, 55000]
}

df = pd.DataFrame(data)
print("\nPandas DataFrame:")
print(df)
print("\n描述性统计:")
print(df.describe())

# 数据筛选
high_salary = df[df['工资'] > 55000]
print("\n高工资人员:")
print(high_salary)

# Matplotlib - 数据可视化
plt.figure(figsize=(10, 6))

# 散点图
plt.subplot(2, 2, 1)
plt.scatter(df['年龄'], df['工资'])
plt.xlabel('年龄')
plt.ylabel('工资')
plt.title('年龄与工资关系')

# 柱状图
plt.subplot(2, 2, 2)
df['城市'].value_counts().plot(kind='bar')
plt.title('城市分布')

# 箱线图
plt.subplot(2, 2, 3)
plt.boxplot(df['工资'])
plt.title('工资分布')

plt.tight_layout()
plt.savefig('data_visualization.png')
plt.show()

# Scikit-learn - 机器学习示例
X = df[['年龄']]  # 特征
y = df['工资']    # 目标

model = LinearRegression()
model.fit(X, y)

prediction = model.predict([[40]])
print(f"\n预测40岁工资: {prediction[0]:.2f}")

2. Web开发框架

# Flask 示例 (安装: pip install flask)
from flask import Flask, render_template, request, jsonify

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html', title='首页')

@app.route('/api/users', methods=['GET'])
def get_users():
    users = [
        {'id': 1, 'name': '张三', 'email': 'zhang@example.com'},
        {'id': 2, 'name': '李四', 'email': 'li@example.com'}
    ]
    return jsonify(users)

@app.route('/api/users', methods=['POST'])
def create_user():
    data = request.json
    # 这里应该添加数据验证和保存逻辑
    return jsonify({'message': '用户创建成功', 'id': 3}), 201

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)

3. 数据库操作

# SQLAlchemy ORM (安装: pip install sqlalchemy)
from sqlalchemy import create_engine, Column, Integer, String, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from datetime import datetime

# 数据库连接
engine = create_engine('sqlite:///example.db', echo=True)
Base = declarative_base()

# 定义数据模型
class User(Base):
    __tablename__ = 'users'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(50), nullable=False)
    email = Column(String(100), unique=True)
    created_at = Column(DateTime, default=datetime.now)
    
    def __repr__(self):
        return f"<User(name='{self.name}', email='{self.email}')>"

# 创建表
Base.metadata.create_all(engine)

# 创建会话
Session = sessionmaker(bind=engine)
session = Session()

# 数据库操作
try:
    # 添加用户
    new_user = User(name="王五", email="wang@example.com")
    session.add(new_user)
    session.commit()
    
    # 查询用户
    users = session.query(User).all()
    print("所有用户:", users)
    
    # 更新用户
    user = session.query(User).filter_by(name="王五").first()
    if user:
        user.email = "wangwu@example.com"
        session.commit()
    
    # 删除用户
    # session.delete(user)
    # session.commit()
    
except Exception as e:
    session.rollback()
    print("数据库错误:", e)
finally:
    session.close()

4. 网络请求和API调用

# Requests 库 (安装: pip install requests)
import requests
import json

class APIClient:
    def __init__(self, base_url):
        self.base_url = base_url
        self.session = requests.Session()
        self.session.headers.update({
            'User-Agent': 'MyApp/1.0',
            'Content-Type': 'application/json'
        })
    
    def get(self, endpoint, params=None):
        """GET 请求"""
        url = f"{self.base_url}/{endpoint}"
        try:
            response = self.session.get(url, params=params, timeout=10)
            response.raise_for_status()  # 检查HTTP错误
            return response.json()
        except requests.exceptions.RequestException as e:
            print(f"请求错误: {e}")
            return None
    
    def post(self, endpoint, data):
        """POST 请求"""
        url = f"{self.base_url}/{endpoint}"
        try:
            response = self.session.post(url, json=data, timeout=10)
            response.raise_for_status()
            return response.json()
        except requests.exceptions.RequestException as e:
            print(f"请求错误: {e}")
            return None

# 使用示例
if __name__ == "__main__":
    # 使用公开的测试API
    client = APIClient("https://jsonplaceholder.typicode.com")
    
    # 获取数据
    users = client.get("users")
    if users:
        print("获取到的用户数据:")
        for user in users[:3]:  # 只显示前3个
            print(f"- {user['name']} ({user['email']})")
    
    # 发送数据
    new_post = {
        "title": "测试标题",
        "body": "测试正文内容",
        "userId": 1
    }
    
    result = client.post("posts", new_post)
    if result:
        print("创建的文章ID:", result.get('id'))

5. 测试框架

# Pytest 测试示例 (安装: pip install pytest)
import pytest

# 要测试的函数
def add_numbers(a, b):
    """相加两个数字"""
    if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
        raise TypeError("参数必须是数字")
    return a + b

def divide_numbers(a, b):
    """相除两个数字"""
    if b == 0:
        raise ValueError("除数不能为零")
    return a / b

# 测试类
class TestMathOperations:
    """数学运算测试类"""
    
    def test_add_positive_numbers(self):
        """测试正数相加"""
        assert add_numbers(2, 3) == 5
        assert add_numbers(0, 0) == 0
        assert add_numbers(-1, 1) == 0
    
    def test_add_negative_numbers(self):
        """测试负数相加"""
        assert add_numbers(-2, -3) == -5
        assert add_numbers(-5, 3) == -2
    
    def test_add_invalid_input(self):
        """测试无效输入"""
        with pytest.raises(TypeError):
            add_numbers("2", 3)
        with pytest.raises(TypeError):
            add_numbers(2, "3")
    
    def test_divide_numbers(self):
        """测试除法"""
        assert divide_numbers(10, 2) == 5
        assert divide_numbers(5, 2) == 2.5
    
    def test_divide_by_zero(self):
        """测试除零错误"""
        with pytest.raises(ValueError):
            divide_numbers(10, 0)

# 参数化测试
@pytest.mark.parametrize("a,b,expected", [
    (1, 1, 2),
    (2, 3, 5),
    (-1, 1, 0),
    (0, 0, 0)
])
def test_add_with_parameters(a, b, expected):
    """参数化加法测试"""
    assert add_numbers(a, b) == expected

# 夹具 (fixture)
@pytest.fixture
def sample_data():
    """提供测试数据"""
    return {'a': 10, 'b': 5}

def test_with_fixture(sample_data):
    """使用夹具的测试"""
    result = add_numbers(sample_data['a'], sample_data['b'])
    assert result == 15

# 运行测试 (在命令行中运行: pytest 此文件名.py -v)

三、虚拟环境和依赖管理

1. 虚拟环境使用

# 创建虚拟环境
python -m venv myproject_env

# 激活虚拟环境
# Windows:
myproject_env\Scripts\activate
# macOS/Linux:
source myproject_env/bin/activate

# 安装包
pip install requests pandas numpy

# 生成requirements.txt
pip freeze > requirements.txt

# 从requirements.txt安装
pip install -r requirements.txt

# 退出虚拟环境
deactivate

2. 依赖管理文件示例

# requirements.txt
requests>=2.25.0
pandas>=1.3.0
numpy>=1.21.0
flask>=2.0.0
sqlalchemy>=1.4.0
pytest>=6.0.0
black>=21.0.0
flake8>=3.9.0

3. 使用 pyproject.toml (现代Python项目)

# pyproject.toml
[project]
name = "my-project"
version = "0.1.0"
description = "我的Python项目"
authors = [
    {name = "Your Name", email = "your.email@example.com"}
]
dependencies = [
    "requests>=2.25.0",
    "pandas>=1.3.0",
    "flask>=2.0.0",
]

[project.optional-dependencies]
dev = [
    "pytest>=6.0.0",
    "black>=21.0.0",
    "flake8>=3.9.0"
]

[build-system]
requires = ["setuptools>=61.0.0", "wheel"]
build-backend = "setuptools.build_meta"

[tool.black]
line-length = 88
target-version = ['py39']

四、实用工具和技巧

1. 日志记录

import logging
import logging.config

# 配置日志
logging.config.dictConfig({
    'version': 1,
    'formatters': {
        'detailed': {
            'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
        }
    },
    'handlers': {
        'file': {
            'class': 'logging.FileHandler',
            'filename': 'app.log',
            'formatter': 'detailed',
            'level': 'DEBUG'
        },
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'detailed',
            'level': 'INFO'
        }
    },
    'root': {
        'level': 'DEBUG',
        'handlers': ['file', 'console']
    }
})

# 使用日志
logger = logging.getLogger(__name__)

def risky_operation(x, y):
    try:
        result = x / y
        logger.info(f"计算成功: {x} / {y} = {result}")
        return result
    except ZeroDivisionError:
        logger.error("除零错误")
        return None
    except Exception as e:
        logger.exception("未预期的错误")
        return None

# 测试日志
risky_operation(10, 2)
risky_operation(10, 0)

2. 配置管理

import configparser
from dataclasses import dataclass
from typing import Dict, Any

@dataclass
class DatabaseConfig:
    host: str
    port: int
    username: str
    password: str
    database: str

@dataclass
class AppConfig:
    debug: bool
    host: str
    port: int
    database: DatabaseConfig

class ConfigManager:
    def __init__(self, config_file='config.ini'):
        self.config_file = config_file
        self.config = configparser.ConfigParser()
        self.config.read(config_file, encoding='utf-8')
    
    def get_database_config(self) -> DatabaseConfig:
        return DatabaseConfig(
            host=self.config['database']['host'],
            port=int(self.config['database']['port']),
            username=self.config['database']['username'],
            password=self.config['database']['password'],
            database=self.config['database']['database']
        )
    
    def get_app_config(self) -> AppConfig:
        return AppConfig(
            debug=self.config.getboolean('app', 'debug', fallback=False),
            host=self.config['app']['host'],
            port=int(self.config['app']['port']),
            database=self.get_database_config()
        )
    
    def save_config(self, config_dict: Dict[str, Any]):
        """保存配置到文件"""
        for section, options in config_dict.items():
            if section not in self.config:
                self.config.add_section(section)
            for key, value in options.items():
                self.config[section][key] = str(value)
        
        with open(self.config_file, 'w', encoding='utf-8') as f:
            self.config.write(f)

# 使用示例
config = ConfigManager('config.ini')
app_config = config.get_app_config()
print(f"应用运行在: {app_config.host}:{app_config.port}")
print(f"数据库连接: {app_config.database.host}:{app_config.database.port}")

这个详细的指南涵盖了Python标准库的主要模块和常用的第三方库。掌握这些库将大大提高您的Python开发效率。建议根据实际项目需求选择学习相应的库,并通过实际项目来巩固这些知识。

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐