Python的强大之处在于其丰富的标准库和庞大的第三方库生态系统。本章将深入学习常用的标准库,以及如何安装、使用和管理第三方库。

10.1 常用标准库详解

时间和日期处理

import datetime
import time
import calendar
from dateutil import parser, relativedelta
import pytz

def datetime_operations():
    """时间和日期操作示例"""
    
    print("=== 时间和日期操作 ===")
    
    # 1. datetime基础操作
    print("\n--- datetime基础操作 ---")
    
    # 获取当前时间
    now = datetime.datetime.now()
    utc_now = datetime.datetime.utcnow()
    
    print(f"当前本地时间: {now}")
    print(f"当前UTC时间: {utc_now}")
    print(f"时间戳: {now.timestamp()}")
    
    # 创建特定时间
    specific_date = datetime.datetime(2023, 12, 25, 15, 30, 45)
    print(f"特定时间: {specific_date}")
    
    # 日期和时间分离
    today = datetime.date.today()
    current_time = datetime.time(14, 30, 0)
    
    print(f"今天日期: {today}")
    print(f"当前时间: {current_time}")
    
    # 2. 时间格式化和解析
    print("\n--- 时间格式化和解析 ---")
    
    # 格式化时间
    formatted_time = now.strftime("%Y-%m-%d %H:%M:%S")
    chinese_format = now.strftime("%Y年%m月%d日 %H时%M分%S秒")
    iso_format = now.isoformat()
    
    print(f"标准格式: {formatted_time}")
    print(f"中文格式: {chinese_format}")
    print(f"ISO格式: {iso_format}")
    
    # 解析时间字符串
    time_str = "2023-12-25 15:30:45"
    parsed_time = datetime.datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
    print(f"解析时间: {parsed_time}")
    
    # 3. 时间计算
    print("\n--- 时间计算 ---")
    
    # 时间差计算
    future_date = now + datetime.timedelta(days=30, hours=5, minutes=30)
    past_date = now - datetime.timedelta(weeks=2)
    
    print(f"30天5小时30分钟后: {future_date}")
    print(f"2周前: {past_date}")
    
    # 计算时间差
    time_diff = future_date - now
    print(f"时间差: {time_diff}")
    print(f"总秒数: {time_diff.total_seconds()}")
    print(f"总天数: {time_diff.days}")
    
    # 4. 时区处理
    print("\n--- 时区处理 ---")
    
    try:
        # 创建带时区的时间
        beijing_tz = pytz.timezone('Asia/Shanghai')
        tokyo_tz = pytz.timezone('Asia/Tokyo')
        utc_tz = pytz.UTC
        
        # 本地时间转换为带时区时间
        beijing_time = beijing_tz.localize(now.replace(tzinfo=None))
        print(f"北京时间: {beijing_time}")
        
        # 时区转换
        tokyo_time = beijing_time.astimezone(tokyo_tz)
        utc_time = beijing_time.astimezone(utc_tz)
        
        print(f"东京时间: {tokyo_time}")
        print(f"UTC时间: {utc_time}")
        
        # 获取时区信息
        print(f"北京时区偏移: {beijing_time.utcoffset()}")
        print(f"时区名称: {beijing_time.tzname()}")
        
    except ImportError:
        print("需要安装pytz: pip install pytz")
    
    # 5. 日历操作
    print("\n--- 日历操作 ---")
    
    # 获取月历
    year, month = 2023, 12
    month_calendar = calendar.month(year, month)
    print(f"{year}年{month}月日历:")
    print(month_calendar)
    
    # 获取月份信息
    month_range = calendar.monthrange(year, month)
    print(f"{year}年{month}月第一天是星期{month_range[0]},共{month_range[1]}天")
    
    # 判断闰年
    is_leap = calendar.isleap(year)
    print(f"{year}年是闰年: {is_leap}")
    
    # 获取星期几
    weekday = calendar.weekday(year, month, 25)
    weekday_name = calendar.day_name[weekday]
    print(f"{year}-{month}-25是星期{weekday_name}")
    
    # 6. 高级日期操作
    print("\n--- 高级日期操作 ---")
    
    try:
        from dateutil import parser, relativedelta
        
        # 智能日期解析
        date_strings = [
            "2023-12-25",
            "Dec 25, 2023",
            "25/12/2023",
            "2023年12月25日"
        ]
        
        for date_str in date_strings:
            try:
                parsed = parser.parse(date_str)
                print(f"解析 '{date_str}' -> {parsed}")
            except:
                print(f"无法解析: {date_str}")
        
        # 相对时间计算
        base_date = datetime.datetime(2023, 1, 31)
        
        # 加一个月(智能处理月末)
        next_month = base_date + relativedelta.relativedelta(months=1)
        print(f"\n{base_date.date()} + 1个月 = {next_month.date()}")
        
        # 加一年
        next_year = base_date + relativedelta.relativedelta(years=1)
        print(f"{base_date.date()} + 1年 = {next_year.date()}")
        
        # 获取下个星期一
        next_monday = base_date + relativedelta.relativedelta(weekday=relativedelta.MO(1))
        print(f"{base_date.date()} 的下个星期一: {next_monday.date()}")
        
        # 获取月末
        month_end = base_date + relativedelta.relativedelta(day=31)
        print(f"{base_date.date()} 的月末: {month_end.date()}")
        
    except ImportError:
        print("需要安装python-dateutil: pip install python-dateutil")
    
    # 7. 性能测试和时间测量
    print("\n--- 性能测试 ---")
    
    # 使用time模块测量执行时间
    start_time = time.time()
    
    # 模拟一些计算
    result = sum(i**2 for i in range(100000))
    
    end_time = time.time()
    execution_time = end_time - start_time
    
    print(f"计算结果: {result}")
    print(f"执行时间: {execution_time:.4f} 秒")
    
    # 使用perf_counter获得更精确的时间
    start_perf = time.perf_counter()
    
    # 模拟计算
    time.sleep(0.1)  # 休眠100毫秒
    
    end_perf = time.perf_counter()
    perf_time = end_perf - start_perf
    
    print(f"精确测量时间: {perf_time:.4f} 秒")
    
    # 8. 定时器和调度
    print("\n--- 定时器示例 ---")
    
    import threading
    
    def timer_callback():
        print(f"定时器触发: {datetime.datetime.now()}")
    
    # 创建定时器(5秒后执行)
    timer = threading.Timer(2.0, timer_callback)
    timer.start()
    
    print("定时器已启动,2秒后触发")
    
    # 等待定时器完成
    timer.join()

datetime_operations()

# 正则表达式
def regex_operations():
    """正则表达式操作示例"""
    
    print("\n=== 正则表达式操作 ===")
    
    import re
    
    # 1. 基本匹配
    print("\n--- 基本匹配 ---")
    
    text = "我的电话是 138-1234-5678,邮箱是 user@example.com"
    
    # 查找电话号码
    phone_pattern = r'\d{3}-\d{4}-\d{4}'
    phone_match = re.search(phone_pattern, text)
    
    if phone_match:
        print(f"找到电话号码: {phone_match.group()}")
        print(f"位置: {phone_match.start()}-{phone_match.end()}")
    
    # 查找邮箱
    email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
    email_match = re.search(email_pattern, text)
    
    if email_match:
        print(f"找到邮箱: {email_match.group()}")
    
    # 2. 查找所有匹配
    print("\n--- 查找所有匹配 ---")
    
    text_multiple = """联系方式:
    张三: 138-1234-5678, zhang@company.com
    李四: 139-8765-4321, li@company.com
    王五: 137-5555-6666, wang@company.com
    """
    
    # 查找所有电话号码
    all_phones = re.findall(phone_pattern, text_multiple)
    print(f"所有电话号码: {all_phones}")
    
    # 查找所有邮箱
    all_emails = re.findall(email_pattern, text_multiple)
    print(f"所有邮箱: {all_emails}")
    
    # 使用finditer获取详细信息
    print("\n详细匹配信息:")
    for match in re.finditer(email_pattern, text_multiple):
        print(f"  邮箱: {match.group()}, 位置: {match.start()}-{match.end()}")
    
    # 3. 分组捕获
    print("\n--- 分组捕获 ---")
    
    # 捕获姓名、电话和邮箱
    contact_pattern = r'(\w+):\s*(\d{3}-\d{4}-\d{4}),\s*([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})'
    
    for match in re.finditer(contact_pattern, text_multiple):
        name, phone, email = match.groups()
        print(f"姓名: {name}, 电话: {phone}, 邮箱: {email}")
    
    # 命名分组
    named_pattern = r'(?P<name>\w+):\s*(?P<phone>\d{3}-\d{4}-\d{4}),\s*(?P<email>[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})'
    
    print("\n使用命名分组:")
    for match in re.finditer(named_pattern, text_multiple):
        print(f"姓名: {match.group('name')}")
        print(f"电话: {match.group('phone')}")
        print(f"邮箱: {match.group('email')}")
        print("---")
    
    # 4. 字符串替换
    print("\n--- 字符串替换 ---")
    
    # 简单替换
    text_replace = "今天是2023年12月25日,明天是2023年12月26日"
    
    # 替换年份
    new_text = re.sub(r'2023', '2024', text_replace)
    print(f"替换年份: {new_text}")
    
    # 使用分组进行复杂替换
    date_text = "生日:1990-05-15,入职:2020-03-01"
    
    # 将日期格式从 YYYY-MM-DD 改为 DD/MM/YYYY
    date_pattern = r'(\d{4})-(\d{2})-(\d{2})'
    formatted_text = re.sub(date_pattern, r'\3/\2/\1', date_text)
    print(f"格式化日期: {formatted_text}")
    
    # 使用函数进行替换
    def date_formatter(match):
        year, month, day = match.groups()
        return f"{day}年{month}月{year}日"
    
    chinese_date = re.sub(date_pattern, date_formatter, date_text)
    print(f"中文日期: {chinese_date}")
    
    # 5. 字符串分割
    print("\n--- 字符串分割 ---")
    
    # 使用多种分隔符分割
    mixed_text = "apple,banana;orange:grape|watermelon"
    fruits = re.split(r'[,;:|]', mixed_text)
    print(f"水果列表: {fruits}")
    
    # 保留分隔符
    text_with_separators = re.split(r'([,;:|])', mixed_text)
    print(f"保留分隔符: {text_with_separators}")
    
    # 6. 编译正则表达式
    print("\n--- 编译正则表达式 ---")
    
    # 编译常用的正则表达式以提高性能
    email_regex = re.compile(email_pattern)
    phone_regex = re.compile(phone_pattern)
    
    test_texts = [
        "联系我:john@example.com",
        "电话:138-1234-5678",
        "无效文本",
        "邮箱:admin@test.org,电话:139-8765-4321"
    ]
    
    for text in test_texts:
        email_found = email_regex.search(text)
        phone_found = phone_regex.search(text)
        
        print(f"文本: {text}")
        print(f"  邮箱: {email_found.group() if email_found else '未找到'}")
        print(f"  电话: {phone_found.group() if phone_found else '未找到'}")
    
    # 7. 高级正则表达式特性
    print("\n--- 高级特性 ---")
    
    # 前瞻和后顾断言
    password_text = "密码:abc123, password123, 123456, Abc123!"
    
    # 匹配包含字母和数字的密码(前瞻断言)
    strong_password_pattern = r'\b(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d!@#$%^&*]{6,}\b'
    strong_passwords = re.findall(strong_password_pattern, password_text)
    print(f"强密码: {strong_passwords}")
    
    # 非贪婪匹配
    html_text = "<div>内容1</div><div>内容2</div>"
    
    # 贪婪匹配(默认)
    greedy_match = re.findall(r'<div>.*</div>', html_text)
    print(f"贪婪匹配: {greedy_match}")
    
    # 非贪婪匹配
    non_greedy_match = re.findall(r'<div>.*?</div>', html_text)
    print(f"非贪婪匹配: {non_greedy_match}")
    
    # 8. 实用正则表达式模式
    print("\n--- 实用模式 ---")
    
    patterns = {
        '中国手机号': r'^1[3-9]\d{9}$',
        '身份证号': r'^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$',
        'IP地址': r'^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$',
        'URL': r'^https?://[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?$',
        '中文字符': r'[\u4e00-\u9fa5]+',
        '银行卡号': r'^[1-9]\d{15,18}$'
    }
    
    test_data = {
        '中国手机号': ['13812345678', '12345678901', '138123456789'],
        'IP地址': ['192.168.1.1', '256.1.1.1', '127.0.0.1'],
        'URL': ['https://www.example.com', 'http://test.com/path', 'ftp://invalid'],
        '中文字符': ['Hello 世界', '123', '你好世界']
    }
    
    for pattern_name, pattern in patterns.items():
        if pattern_name in test_data:
            print(f"\n{pattern_name} 验证:")
            regex = re.compile(pattern)
            
            for test_value in test_data[pattern_name]:
                is_match = bool(regex.match(test_value) if pattern_name != '中文字符' else regex.search(test_value))
                print(f"  {test_value}: {'✓' if is_match else '✗'}")

regex_operations()

运行时间和正则表达式示例:

python datetime_regex.py

数学和随机数

import math
import random
import statistics
import decimal
import fractions
from collections import Counter

def math_operations():
    """数学运算示例"""
    
    print("\n=== 数学运算 ===")
    
    # 1. 基本数学函数
    print("\n--- 基本数学函数 ---")
    
    numbers = [16, 25, 100, -5, 3.14159, 2.71828]
    
    for num in numbers:
        print(f"\n数字: {num}")
        
        if num > 0:
            print(f"  平方根: {math.sqrt(num):.4f}")
            print(f"  对数(自然): {math.log(num):.4f}")
            print(f"  对数(10): {math.log10(num):.4f}")
        
        print(f"  绝对值: {abs(num)}")
        print(f"  向上取整: {math.ceil(num)}")
        print(f"  向下取整: {math.floor(num)}")
        print(f"  四舍五入: {round(num, 2)}")
        print(f"  平方: {num ** 2}")
    
    # 2. 三角函数
    print("\n--- 三角函数 ---")
    
    angles_degrees = [0, 30, 45, 60, 90, 180, 360]
    
    print("角度\t弧度\t\tsin\t\tcos\t\ttan")
    print("-" * 60)
    
    for angle in angles_degrees:
        radians = math.radians(angle)
        sin_val = math.sin(radians)
        cos_val = math.cos(radians)
        
        # 处理tan在90度和270度的无穷大情况
        if angle in [90, 270]:
            tan_val = float('inf') if angle == 90 else float('-inf')
            tan_str = "∞"
        else:
            tan_val = math.tan(radians)
            tan_str = f"{tan_val:.4f}"
        
        print(f"{angle}°\t{radians:.4f}\t\t{sin_val:.4f}\t\t{cos_val:.4f}\t\t{tan_str}")
    
    # 3. 指数和幂函数
    print("\n--- 指数和幂函数 ---")
    
    base_numbers = [2, 3, 10]
    exponents = [0, 1, 2, 0.5, -1]
    
    for base in base_numbers:
        print(f"\n底数 {base}:")
        for exp in exponents:
            result = math.pow(base, exp)
            print(f"  {base}^{exp} = {result:.4f}")
    
    # e的幂
    print(f"\ne^1 = {math.e:.6f}")
    print(f"e^2 = {math.exp(2):.6f}")
    print(f"e^π = {math.exp(math.pi):.6f}")
    
    # 4. 统计函数
    print("\n--- 统计函数 ---")
    
    data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4]
    
    print(f"数据: {data}")
    print(f"平均值: {statistics.mean(data):.4f}")
    print(f"中位数: {statistics.median(data):.4f}")
    print(f"众数: {statistics.mode(data)}")
    
    try:
        print(f"标准差: {statistics.stdev(data):.4f}")
        print(f"方差: {statistics.variance(data):.4f}")
    except statistics.StatisticsError as e:
        print(f"统计错误: {e}")
    
    # 分位数
    print(f"第一四分位数: {statistics.quantiles(data, n=4)[0]:.4f}")
    print(f"第三四分位数: {statistics.quantiles(data, n=4)[2]:.4f}")
    
    # 5. 精确小数计算
    print("\n--- 精确小数计算 ---")
    
    # 浮点数精度问题
    print("浮点数精度问题:")
    print(f"0.1 + 0.2 = {0.1 + 0.2}")
    print(f"0.1 + 0.2 == 0.3: {0.1 + 0.2 == 0.3}")
    
    # 使用Decimal解决精度问题
    print("\n使用Decimal:")
    decimal.getcontext().prec = 10  # 设置精度
    
    d1 = decimal.Decimal('0.1')
    d2 = decimal.Decimal('0.2')
    d3 = decimal.Decimal('0.3')
    
    print(f"Decimal('0.1') + Decimal('0.2') = {d1 + d2}")
    print(f"结果等于0.3: {d1 + d2 == d3}")
    
    # 金融计算示例
    price = decimal.Decimal('19.99')
    tax_rate = decimal.Decimal('0.08')
    tax = price * tax_rate
    total = price + tax
    
    print(f"\n金融计算:")
    print(f"商品价格: ${price}")
    print(f"税率: {tax_rate * 100}%")
    print(f"税额: ${tax:.2f}")
    print(f"总价: ${total:.2f}")
    
    # 6. 分数计算
    print("\n--- 分数计算 ---")
    
    # 创建分数
    frac1 = fractions.Fraction(1, 3)
    frac2 = fractions.Fraction(2, 5)
    frac3 = fractions.Fraction('0.125')  # 从小数创建
    
    print(f"分数1: {frac1} = {float(frac1):.6f}")
    print(f"分数2: {frac2} = {float(frac2):.6f}")
    print(f"分数3: {frac3} = {float(frac3):.6f}")
    
    # 分数运算
    print(f"\n分数运算:")
    print(f"{frac1} + {frac2} = {frac1 + frac2}")
    print(f"{frac1} - {frac2} = {frac1 - frac2}")
    print(f"{frac1} * {frac2} = {frac1 * frac2}")
    print(f"{frac1} / {frac2} = {frac1 / frac2}")
    
    # 7. 组合数学
    print("\n--- 组合数学 ---")
    
    n, k = 10, 3
    
    # 排列
    permutations = math.perm(n, k)
    print(f"P({n}, {k}) = {permutations}")
    
    # 组合
    combinations = math.comb(n, k)
    print(f"C({n}, {k}) = {combinations}")
    
    # 阶乘
    factorial_5 = math.factorial(5)
    print(f"5! = {factorial_5}")
    
    # 最大公约数和最小公倍数
    a, b = 48, 18
    gcd_val = math.gcd(a, b)
    lcm_val = abs(a * b) // gcd_val
    
    print(f"\ngcd({a}, {b}) = {gcd_val}")
    print(f"lcm({a}, {b}) = {lcm_val}")

math_operations()

# 随机数生成
def random_operations():
    """随机数操作示例"""
    
    print("\n=== 随机数操作 ===")
    
    # 1. 基本随机数生成
    print("\n--- 基本随机数生成 ---")
    
    # 设置随机种子(用于可重现的结果)
    random.seed(42)
    
    # 生成0-1之间的随机浮点数
    print("0-1之间的随机数:")
    for i in range(5):
        print(f"  {random.random():.6f}")
    
    # 生成指定范围的随机整数
    print("\n1-100之间的随机整数:")
    for i in range(5):
        print(f"  {random.randint(1, 100)}")
    
    # 生成指定范围的随机浮点数
    print("\n10.0-20.0之间的随机浮点数:")
    for i in range(5):
        print(f"  {random.uniform(10.0, 20.0):.4f}")
    
    # 2. 序列随机操作
    print("\n--- 序列随机操作 ---")
    
    # 随机选择
    colors = ['红', '绿', '蓝', '黄', '紫', '橙']
    print(f"颜色列表: {colors}")
    
    # 随机选择一个元素
    chosen_color = random.choice(colors)
    print(f"随机选择: {chosen_color}")
    
    # 随机选择多个元素(有重复)
    chosen_colors = random.choices(colors, k=3)
    print(f"随机选择3个(可重复): {chosen_colors}")
    
    # 随机选择多个元素(无重复)
    sample_colors = random.sample(colors, 3)
    print(f"随机抽样3个(不重复): {sample_colors}")
    
    # 打乱列表
    numbers = list(range(1, 11))
    print(f"\n原始列表: {numbers}")
    
    random.shuffle(numbers)
    print(f"打乱后: {numbers}")
    
    # 3. 概率分布
    print("\n--- 概率分布 ---")
    
    # 正态分布
    print("正态分布(均值=0,标准差=1):")
    normal_samples = [random.gauss(0, 1) for _ in range(10)]
    for i, sample in enumerate(normal_samples):
        print(f"  样本{i+1}: {sample:.4f}")
    
    # 指数分布
    print("\n指数分布(λ=1):")
    exp_samples = [random.expovariate(1) for _ in range(5)]
    for i, sample in enumerate(exp_samples):
        print(f"  样本{i+1}: {sample:.4f}")
    
    # 4. 加权随机选择
    print("\n--- 加权随机选择 ---")
    
    items = ['A', 'B', 'C', 'D']
    weights = [10, 20, 30, 40]  # 权重
    
    print(f"项目: {items}")
    print(f"权重: {weights}")
    
    # 进行1000次加权随机选择
    results = [random.choices(items, weights=weights)[0] for _ in range(1000)]
    counter = Counter(results)
    
    print("\n1000次选择结果:")
    for item in items:
        count = counter[item]
        percentage = (count / 1000) * 100
        expected = (weights[items.index(item)] / sum(weights)) * 100
        print(f"  {item}: {count}次 ({percentage:.1f}%, 期望{expected:.1f}%)")
    
    # 5. 随机密码生成
    print("\n--- 随机密码生成 ---")
    
    import string
    
    def generate_password(length=12, include_symbols=True):
        """生成随机密码"""
        characters = string.ascii_letters + string.digits
        if include_symbols:
            characters += string.punctuation
        
        password = ''.join(random.choice(characters) for _ in range(length))
        return password
    
    print("随机密码:")
    for i in range(5):
        password = generate_password(12, True)
        print(f"  密码{i+1}: {password}")
    
    # 6. 蒙特卡洛方法估算π
    print("\n--- 蒙特卡洛估算π ---")
    
    def estimate_pi(num_points=100000):
        """使用蒙特卡洛方法估算π"""
        inside_circle = 0
        
        for _ in range(num_points):
            x = random.uniform(-1, 1)
            y = random.uniform(-1, 1)
            
            # 检查点是否在单位圆内
            if x*x + y*y <= 1:
                inside_circle += 1
        
        # π ≈ 4 * (圆内点数 / 总点数)
        pi_estimate = 4 * inside_circle / num_points
        return pi_estimate
    
    estimated_pi = estimate_pi(100000)
    actual_pi = math.pi
    error = abs(estimated_pi - actual_pi)
    
    print(f"蒙特卡洛估算π: {estimated_pi:.6f}")
    print(f"实际π值: {actual_pi:.6f}")
    print(f"误差: {error:.6f}")
    
    # 7. 随机数据生成
    print("\n--- 随机数据生成 ---")
    
    # 生成随机用户数据
    first_names = ['张', '李', '王', '刘', '陈', '杨', '赵', '黄', '周', '吴']
    last_names = ['伟', '芳', '娜', '敏', '静', '丽', '强', '磊', '军', '洋']
    
    def generate_random_user():
        """生成随机用户数据"""
        name = random.choice(first_names) + random.choice(last_names)
        age = random.randint(18, 65)
        salary = random.randint(3000, 20000)
        department = random.choice(['技术部', '销售部', '市场部', '人事部', '财务部'])
        
        return {
            'name': name,
            'age': age,
            'salary': salary,
            'department': department
        }
    
    print("随机用户数据:")
    for i in range(5):
        user = generate_random_user()
        print(f"  用户{i+1}: {user}")

random_operations()

运行数学和随机数示例:

python math_random.py

10.2 第三方库管理

包管理工具详解

# pip_management.py
"""
Python包管理最佳实践

本文件演示如何使用pip和其他工具管理Python包
"""

import subprocess
import sys
import json
from pathlib import Path

def pip_operations():
    """pip操作示例"""
    
    print("=== pip包管理 ===")
    
    # 1. 基本pip命令(这些命令需要在终端中运行)
    print("\n--- 基本pip命令 ---")
    
    pip_commands = {
        "安装包": "pip install package_name",
        "安装特定版本": "pip install package_name==1.2.3",
        "安装最新版本": "pip install --upgrade package_name",
        "卸载包": "pip uninstall package_name",
        "列出已安装包": "pip list",
        "显示包信息": "pip show package_name",
        "搜索包": "pip search keyword",
        "生成依赖文件": "pip freeze > requirements.txt",
        "从文件安装": "pip install -r requirements.txt",
        "安装开发版本": "pip install -e .",
        "从git安装": "pip install git+https://github.com/user/repo.git"
    }
    
    for description, command in pip_commands.items():
        print(f"{description}: {command}")
    
    # 2. 程序化包管理
    print("\n--- 程序化包管理 ---")
    
    def run_pip_command(command):
        """运行pip命令"""
        try:
            result = subprocess.run(
                [sys.executable, '-m', 'pip'] + command.split(),
                capture_output=True,
                text=True,
                check=True
            )
            return result.stdout
        except subprocess.CalledProcessError as e:
            return f"错误: {e.stderr}"
    
    # 列出已安装的包
    print("当前已安装的包:")
    installed_packages = run_pip_command("list --format=json")
    
    try:
        packages = json.loads(installed_packages)
        for pkg in packages[:10]:  # 只显示前10个
            print(f"  {pkg['name']}: {pkg['version']}")
        if len(packages) > 10:
            print(f"  ... 还有 {len(packages) - 10} 个包")
    except json.JSONDecodeError:
        print("  无法解析包列表")
    
    # 3. requirements.txt管理
    print("\n--- requirements.txt管理 ---")
    
    def create_requirements_file():
        """创建requirements.txt文件"""
        requirements_content = """
# 核心依赖
numpy>=1.20.0
pandas>=1.3.0
requests>=2.25.0

# 开发依赖
pytest>=6.0.0
black>=21.0.0
flake8>=3.8.0

# 可选依赖
matplotlib>=3.3.0  # 用于绘图
scipy>=1.7.0       # 科学计算

# 固定版本(用于生产环境)
Django==4.2.0
Flask==2.3.0
        """.strip()
        
        requirements_file = Path("requirements.txt")
        requirements_file.write_text(requirements_content)
        print(f"创建了 {requirements_file.name}")
        
        return requirements_file
    
    def create_dev_requirements():
        """创建开发环境requirements文件"""
        dev_requirements = """
# 包含基础依赖
-r requirements.txt

# 开发工具
jupyter>=1.0.0
ipython>=7.0.0

# 测试工具
pytest-cov>=2.10.0
pytest-mock>=3.6.0

# 代码质量
mypy>=0.910
pylint>=2.9.0

# 文档生成
sphinx>=4.0.0
sphinx-rtd-theme>=0.5.0
        """.strip()
        
        dev_file = Path("requirements-dev.txt")
        dev_file.write_text(dev_requirements)
        print(f"创建了 {dev_file.name}")
        
        return dev_file
    
    # 创建示例requirements文件
    req_file = create_requirements_file()
    dev_req_file = create_dev_requirements()
    
    print("\nrequirements.txt 内容:")
    print(req_file.read_text())
    
    # 4. 虚拟环境管理
    print("\n--- 虚拟环境管理 ---")
    
    venv_commands = {
        "创建虚拟环境": "python -m venv myenv",
        "激活虚拟环境(Windows)": "myenv\\Scripts\\activate",
        "激活虚拟环境(Linux/Mac)": "source myenv/bin/activate",
        "停用虚拟环境": "deactivate",
        "删除虚拟环境": "rm -rf myenv"
    }
    
    print("虚拟环境命令:")
    for description, command in venv_commands.items():
        print(f"  {description}: {command}")
    
    # 5. conda环境管理
    print("\n--- conda环境管理 ---")
    
    conda_commands = {
        "创建环境": "conda create -n myenv python=3.9",
        "激活环境": "conda activate myenv",
        "停用环境": "conda deactivate",
        "列出环境": "conda env list",
        "删除环境": "conda env remove -n myenv",
        "导出环境": "conda env export > environment.yml",
        "从文件创建环境": "conda env create -f environment.yml",
        "安装包": "conda install package_name",
        "从conda-forge安装": "conda install -c conda-forge package_name"
    }
    
    print("conda命令:")
    for description, command in conda_commands.items():
        print(f"  {description}: {command}")
    
    # 6. poetry项目管理
    print("\n--- Poetry项目管理 ---")
    
    def create_pyproject_toml():
        """创建pyproject.toml文件"""
        pyproject_content = """
[tool.poetry]
name = "my-python-project"
version = "0.1.0"
description = "一个示例Python项目"
authors = ["Your Name <your.email@example.com>"]
readme = "README.md"
packages = [{include = "my_project"}]

[tool.poetry.dependencies]
python = "^3.8"
numpy = "^1.20.0"
pandas = "^1.3.0"
requests = "^2.25.0"

[tool.poetry.group.dev.dependencies]
pytest = "^6.0.0"
black = "^21.0.0"
flake8 = "^3.8.0"
mypy = "^0.910"

[tool.poetry.group.docs.dependencies]
sphinx = "^4.0.0"
sphinx-rtd-theme = "^0.5.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

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

[tool.mypy]
python_version = "3.8"
warn_return_any = true
warn_unused_configs = true
        """.strip()
        
        pyproject_file = Path("pyproject.toml")
        pyproject_file.write_text(pyproject_content)
        print(f"创建了 {pyproject_file.name}")
        
        return pyproject_file
    
    pyproject_file = create_pyproject_toml()
    
    poetry_commands = {
        "初始化项目": "poetry init",
        "安装依赖": "poetry install",
        "添加依赖": "poetry add package_name",
        "添加开发依赖": "poetry add --group dev package_name",
        "移除依赖": "poetry remove package_name",
        "更新依赖": "poetry update",
        "显示依赖": "poetry show",
        "运行命令": "poetry run python script.py",
        "激活shell": "poetry shell",
        "构建包": "poetry build",
        "发布包": "poetry publish"
    }
    
    print("\nPoetry命令:")
    for description, command in poetry_commands.items():
        print(f"  {description}: {command}")
    
    # 7. 包版本管理策略
    print("\n--- 包版本管理策略 ---")
    
    version_strategies = {
        "固定版本": {
            "格式": "package==1.2.3",
            "优点": "完全可重现的构建",
            "缺点": "无法获得bug修复和安全更新",
            "适用": "生产环境"
        },
        "兼容版本": {
            "格式": "package~=1.2.0",
            "含义": ">=1.2.0, <1.3.0",
            "优点": "获得补丁更新",
            "适用": "稳定项目"
        },
        "最小版本": {
            "格式": "package>=1.2.0",
            "优点": "获得所有更新",
            "缺点": "可能引入破坏性变更",
            "适用": "开发环境"
        },
        "范围版本": {
            "格式": "package>=1.2.0,<2.0.0",
            "优点": "平衡稳定性和更新",
            "适用": "大多数项目"
        }
    }
    
    for strategy, info in version_strategies.items():
        print(f"\n{strategy}:")
        for key, value in info.items():
            print(f"  {key}: {value}")
    
    # 清理创建的文件
    for file in [req_file, dev_req_file, pyproject_file]:
        if file.exists():
            file.unlink()
            print(f"\n清理文件: {file.name}")

pip_operations()

运行包管理示例:

python pip_management.py

本章小结

本章我们学习了Python标准库和第三方库的使用:

  1. 常用标准库:datetime、re、math、random、statistics等
  2. 包管理工具:pip、conda、poetry的使用和最佳实践
  3. 虚拟环境:创建隔离的开发环境
  4. 依赖管理:requirements.txt、pyproject.toml等配置文件

下一章预告

下一章我们将学习《项目实战》,内容包括: - 完整项目的设计和实现 - 代码组织和项目结构 - 测试和部署 - 最佳实践和经验总结

练习题

基础练习

  1. 时间处理

    • 实现一个日程管理系统
    • 创建一个时区转换工具
    • 编写一个工作日计算器
  2. 正则表达式

    • 实现一个日志分析器
    • 创建一个数据验证工具
    • 编写一个文本提取器

进阶练习

  1. 数学计算

    • 实现一个科学计算器
    • 创建一个统计分析工具
    • 编写一个数值积分器
  2. 包管理

    • 创建一个自己的Python包
    • 实现一个依赖分析工具
    • 编写一个项目模板生成器

提示:掌握标准库和第三方库的使用是Python开发的重要技能。合理选择和管理依赖,能让你的项目更加稳定和可维护。