🐍Python 工程与虚拟环境
requirements与pyprojecttoml
面试回答
常见问法
requirements.txt 和 pyproject.toml 分别解决什么问题?
回答
requirements.txt更偏“安装依赖清单”pyproject.toml更偏“项目构建与元数据中心”
现代 Python 工程越来越倾向把项目配置、构建后端、依赖声明放进 pyproject.toml,而 requirements.txt 常用于部署、锁定或导出具体安装集合。
追问
- 为什么
pyproject.toml越来越重要 - 它和
setup.py、setup.cfg是什么关系 - 团队里什么时候仍然会保留
requirements.txt
原理展开
requirements.txt 通常长这样:
fastapi==0.115.0
uvicorn==0.30.0
而 pyproject.toml 会描述更多内容,比如:
- 项目名和版本
- Python 版本要求
- 构建工具
- 依赖
- 工具配置
所以面试里可以概括成:
“requirements.txt 更像安装清单,pyproject.toml 更像项目级配置入口。”
易错点
- 把两个文件当成完全替代关系
- 不知道
pyproject.toml不只是依赖文件 - 只会背工具名,不会讲工程分工