摘要:Python虚拟环境是每个开发者的必修课,但工具太多容易选择困难。本文对比venv、virtualenv、conda、poetry、pdm五种方案,帮你根据项目需求选对工具。
为什么需要虚拟环境?
# 没有虚拟环境的噩梦:
# 项目A需要 requests==2.28
# 项目B需要 requests==2.31
# 全局安装只能有一个版本 → 冲突
# 更惨的:
pip install some-package # 不小心把系统Python搞坏了
虚拟环境 = 每个项目有独立的Python和依赖,互不干扰。
方案1:venv(标准库,够用就行)
Python 3.3+ 内置,零依赖:
# 创建虚拟环境
python3 -m venv .venv
# 激活
source .venv/bin/activate # Linux/Mac
# .venv\Scripts\activate # Windows
# 确认
which python # /path/to/.venv/bin/python
pip list # 干净的环境
# 安装依赖
pip install requests flask
# 导出依赖
pip freeze > requirements.txt
# 从requirements.txt安装
pip install -r requirements.txt
# 退出
deactivate
requirements.txt最佳实践:
# requirements.txt - 直接依赖(手动维护)
requests>=2.28,<3.0
flask>=3.0
redis>=5.0
# requirements-dev.txt - 开发依赖
-r requirements.txt
pytest>=7.0
mypy>=1.0
black>=24.0
# 生产环境
pip install -r requirements.txt
# 开发环境
pip install -r requirements-dev.txt
方案2:Poetry(现代项目管理)
依赖管理 + 打包 + 发布一体化:
# 安装
pip install poetry
# 新建项目
poetry new my-project
# 或在已有项目中初始化
poetry init
# 添加依赖
poetry add requests flask
poetry add --group dev pytest mypy black
# 安装所有依赖
poetry install
# 运行命令
poetry run python main.py
poetry run pytest
# 进入虚拟环境shell
poetry shell
# 更新依赖
poetry update
# 导出为requirements.txt(兼容性)
poetry export -f requirements.txt -o requirements.txt
pyproject.toml(Poetry的核心配置):
[tool.poetry]
name = "my-project"
version = "0.1.0"
description = "项目描述"
authors = ["Your Name <you@example.com>"]
[tool.poetry.dependencies]
python = "^3.10"
requests = "^2.31"
flask = "^3.0"
[tool.poetry.group.dev.dependencies]
pytest = "^7.4"
mypy = "^1.8"
black = "^24.1"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
方案3:Conda(数据科学首选)
# 创建环境(可指定Python版本)
conda create -n myenv python=3.11
# 激活
conda activate myenv
# 安装包(优先用conda,找不到再用pip)
conda install numpy pandas scikit-learn
pip install some-package # conda没有的包
# 导出环境
conda env export > environment.yml
# 从yml创建环境
conda env create -f environment.yml
# 查看所有环境
conda env list
# 删除环境
conda env remove -n myenv
environment.yml:
name: myenv
channels:
- conda-forge
- defaults
dependencies:
- python=3.11
- numpy=1.26
- pandas=2.1
- scikit-learn=1.3
- pip:
- some-pip-only-package==1.0
方案4:PDM(新一代,PEP标准)
完全遵循PEP标准,不需要虚拟环境也能隔离:
# 安装
pip install pdm
# 初始化
pdm init
# 添加依赖
pdm add requests flask
pdm add -dG dev pytest mypy
# 安装
pdm install
# 运行
pdm run python main.py
# 更新
pdm update
方案5:uv(最快的包管理器)
Rust写的,速度是pip的10-100倍:
# 安装
pip install uv
# 创建虚拟环境
uv venv
# 安装依赖(极快)
uv pip install requests flask pandas
# 从requirements.txt安装
uv pip install -r requirements.txt
# 编译锁文件
uv pip compile requirements.in -o requirements.txt
怎么选?
| 场景 | 推荐方案 | 理由 |
|---|---|---|
| 简单脚本/小项目 | venv + pip | 零依赖,够用 |
| 正经Python项目 | Poetry | 依赖管理最完善 |
| 数据科学/机器学习 | Conda | 科学计算包预编译 |
| 追求速度 | uv | 安装速度碾压一切 |
| 遵循PEP标准 | PDM | 标准化程度最高 |
我的建议:
- 大多数Web/后端项目 → Poetry
- 数据分析/ML → Conda
- 快速脚本 → venv就够了
- 如果你觉得pip太慢 → 换uv
.gitignore配置
# 虚拟环境
.venv/
venv/
env/
# Poetry
poetry.lock # 库项目不提交,应用项目提交
# Conda
*.conda
# PDM
__pypackages__/
# 通用
*.pyc
__pycache__/
*.egg-info/
dist/
build/
总结
虚拟环境不是可选项,是必选项。哪怕只有一个项目,也要用虚拟环境——因为你永远不知道下一个项目什么时候来,到时候依赖冲突了才想起来就晚了。
选一个顺手的工具,养成习惯,每个项目第一步就是创建虚拟环境。