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 ctypesCython

库名来源核心概念说明
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
解析 HTMLbeautifulsoup4pip 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

练习

以下题目用于验证本章所学内容:

题号题目链接涉及知识点
本章无对应力扣题请用动手练习题自检