Python 第三方库索引 — pip 安装 + python -c 一行流速查
PyPI 上 40 万+ 第三方包,精选约 45 个核心库。以 pip install + python -c "" 一行流为主,面向 C 工程师的工具思维。
全量分类索引见 4库索引 。
网络请求
库名 pip 安装 一行流 说明 requestspip install requestspython -c "import requests; r=requests.get('http://httpbin.org/get'); print(r.json())"HTTP for Humans,比 urllib 简洁 httpxpip install httpxpython -c "import httpx; r=httpx.get('http://httpbin.org/get'); print(r.json())"新一代 HTTP 客户端,支持 async urllib3pip install urllib3python -c "import urllib3; http=urllib3.PoolManager(); r=http.request('GET','http://httpbin.org/get'); print(r.data)"requests 的底层 HTTP 引擎 aiohttppip install aiohttppython -c "import aiohttp,asyncio; async def f(): async with aiohttp.ClientSession() as s: async with s.get('http://httpbin.org/get') as r: print(await r.json()); asyncio.run(f())"异步 HTTP 客户端/服务器
解析与爬虫
库名 pip 安装 一行流 说明 beautifulsoup4pip install beautifulsoup4python -c "from bs4 import BeautifulSoup; soup=BeautifulSoup('<a>hi</a>','html.parser'); print(soup.a.text)"HTML/XML 解析,lxml/html.parser 后端可换 lxmlpip install lxmlpython -c "from lxml import etree; r=etree.fromstring('<a><b>1</b></a>'); print(r.xpath('//b/text()'))"高性能 XML/HTML 解析,XPath 支持 scrapypip install scrapyscrapy shell http://example.com全功能爬虫框架 parselpip install parselpython -c "from parsel import Selector; sel=Selector(text='<a>hi</a>'); print(sel.css('a::text').get())"独立的 CSS/XPath 选择器
命令行工具
库名 pip 安装 一行流 说明 clickpip install clickpython -c "import click; @click.command(); @click.option('--name'); def hello(name): click.echo(f'hi {name}'); hello(['--name','world'])"命令行接口框架,装饰器语法 richpip install richpython -c "from rich import print; print('[red]hello[/red] [green]world[/green]')"终端富文本输出:颜色/表格/进度条/Markdown typerpip install typerpython -c "import typer; app=typer.Typer(); @app.command(); def hello(name:str): typer.echo(f'hi {name}');app(['hello','--name','X'])"基于类型注解的 CLI 框架 tqdmpip install tqdmpython -c "from tqdm import tqdm; import time; [time.sleep(0.01) for _ in tqdm(range(100))]"进度条,包装任意可迭代对象
测试
库名 pip 安装 一行流 说明 pytestpip install pytestpytest test_file.py -v比 unittest 简洁十倍的测试框架 hypothesispip install hypothesispython -c "from hypothesis import given,strategies as st; @given(st.integers());def f(x):assert x==x; f()"基于属性的测试,自动生成用例 coveragepip install coveragecoverage run -m pytest && coverage report代码覆盖率检测
工程
库名 pip 安装 一行流 说明 setuptools内置 python setup.py sdist bdist_wheel打包/分发(setup.py/pyproject.toml) wheelpip install wheelpython -m wheel pack dist/构建 .whl 分发包 buildpip install buildpython -m buildPEP 517 构建前端 twinepip install twinetwine upload dist/*上传包到 PyPI toxpip install toxtox多 Python 版本自动化测试
数据库
库名 来源 一行流 说明 sqlite3标准库 python -c "import sqlite3; con=sqlite3.connect(':memory:'); con.execute('CREATE TABLE t(x)'); con.execute('INSERT INTO t VALUES(1)'); print(con.execute('SELECT * FROM t').fetchall())"内置嵌入式关系数据库 sqlalchemypip install sqlalchemypython -c "from sqlalchemy import create_engine; e=create_engine('sqlite:///:memory:'); print(e.connect().execute('SELECT 1').scalar())"ORM 之王,SQLite/PostgreSQL/MySQL psycopg2pip install psycopg2python -c "import psycopg2; conn=psycopg2.connect('dbname=test'); cur=conn.cursor(); cur.execute('SELECT 1'); print(cur.fetchone())"PostgreSQL 适配器(C libpq 底层) pymongopip install pymongopython -c "from pymongo import MongoClient; c=MongoClient(); print(c.list_database_names())"MongoDB 客户端驱动 redispip install redispython -c "import redis; r=redis.Redis(); r.set('k','v'); print(r.get('k'))"Redis 客户端
安全
库名 pip 安装 一行流 说明 cryptographypip install cryptographypython -c "from cryptography.fernet import Fernet; k=Fernet.generate_key(); f=Fernet(k); print(f.decrypt(f.encrypt(b'hi')))"现代密码学库(对称/非对称/哈希) pyjwtpip install pyjwtpython -c "import jwt; print(jwt.encode({'user':1},'secret',algorithm='HS256'))"JWT 编解码 passlibpip install passlibpython -c "from passlib.hash import pbkdf2_sha256; print(pbkdf2_sha256.hash('password'))"密码哈希(PBKDF2/bcrypt/argon2) bcryptpip install bcryptpython -c "import bcrypt; print(bcrypt.hashpw(b'pwd',bcrypt.gensalt()))"bcrypt 哈希,密码安全存储
跨语言(C ↔ Python)
详见 精通 05 ctypes 和 Cython 。
库名 来源 核心概念 说明 ctypes标准库内置 ctypes.CDLL('./lib.so').func()加载 .so/.dll,调用 C 函数 cffipip install cffifrom _example import ffi; lib = ffi.dlopen('./lib.so')ABI/API 两种模式,类型更灵活 pybind11pip install pybind11C++ 中 PYBIND11_MODULE 宏导出 C++11 模板库,PyTorch 官方采用 cythonpip install cython.pyx 文件编译为 .soPython 超集,可调用 C 函数并编译
快速参考:按需求查找
需求 首选库 pip install 一行流要点 HTTP GET 请求 requestspip install requestsrequests.get(url).json()异步 HTTP 请求 aiohttppip install aiohttpasync with session.get(url) as r解析 HTML beautifulsoup4pip install beautifulsoup4BeautifulSoup(html,'lxml').find('a')写 CLI 工具 clickpip install click@click.command() 装饰器终端彩色输出 richpip install richprint('[red]text[/red]')进度条 tqdmpip install tqdmfor x in tqdm(my_list)单元测试 pytestpip install pytestpytest -v test_file.py覆盖率 coveragepip install coveragecoverage run -m pytest打包发布 build + twinepip install build twinepython -m build && twine upload dist/*SQLite 操作 sqlite3标准库 sqlite3.connect('file.db')ORM 操作 sqlalchemypip install sqlalchemycreate_engine('postgresql://...')加密/解密 cryptographypip install cryptographyFernet(key).encrypt(data)调用 C 库 ctypes标准库 ctypes.CDLL('./lib.so').func()
与其他模块的关联
关联 入口 说明 全量库索引 [[../4库索引 4库索引]] 标准库速查 [[../标准库/标准库索引 标准库索引]] C 互操作 [[../../2精通/05_ctypes:在Python中调用C库 精通 05 ctypes]] 量化分析 6量化分析 NumPy/Pandas/backtrader/akshare 科学计算 7科学计算 SciPy/SymPy 数据可视化 8数据可视化 Matplotlib/Seaborn/Plotly 图形处理 9图形处理 Pillow/OpenCV Web 应用 10web应用 Flask/FastAPI/Django 人工智能 11人工智能 sklearn/PyTorch/TensorFlow
练习
以下题目用于验证本章所学内容:
题号 题目 链接 涉及知识点 — 本章无对应力扣题 — 请用动手练习题自检