环境管理

基础操作

1
2
3
4
5
6
7
8
# 创建指定 Python 版本的环境
conda create -n <env-name> python=3.11

# 创建带依赖的环境(推荐一次性声明)
conda create -n <env-name> python=3.11 pandas numpy scipy

# 创建最小化环境(排除默认包)
conda create -n <env-name> python=3.11 --no-default-packages

环境生命周期管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 激活环境
conda activate <env-name>

# 退出当前环境
conda deactivate

# 查看所有环境(带路径)
conda env list
conda info --envs

# 克隆环境(用于备份或迁移)
conda create -n <new-env> --clone <old-env>

# 删除环境
conda remove -n <env-name> --all

环境变量配置

1
2
3
4
5
6
7
8
# 设置环境变量(仅当前环境)
conda env config vars set MY_VAR=value

# 查看环境变量
conda env config vars list

# 删除环境变量
conda env config vars unset MY_VAR

依赖管理

包安装策略

1
2
3
4
5
6
7
8
9
10
11
# 安装指定版本
conda install <package>=<version>

# 安装多个包(减少依赖解析次数)
conda install numpy pandas matplotlib seaborn

# 从特定 channel 安装(优先使用官方源)
conda install -c conda-forge <package>

# 安装预发布版本
conda install -c conda-forge/label/beta <package>

包查询与维护

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 查看已安装包(含版本和 channel)
conda list

# 查看特定包的详细信息
conda list <package>

# 搜索包(支持模糊匹配)
conda search <package>
conda search <package>=<version>

# 更新包
conda update <package>
conda update --all

# 卸载包(自动清理孤立依赖)
conda remove <package>

依赖冲突排查

1
2
3
4
5
6
7
8
# 检查环境一致性
conda list --explicit

# 查看包的依赖树
conda repoquery depends <package>

# 查看反向依赖(谁依赖了这个包)
conda repoquery whoneeds <package>

环境导出与复现

导出规范

1
2
3
4
5
6
7
8
# 导出完整环境(含版本和 channel,推荐)
conda env export > environment.yml

# 导出最小配置(跨平台兼容)
conda env export --from-history > environment.yml

# 导出精确包列表(仅当前平台)
conda list --explicit > spec-file.txt

环境复现

1
2
3
4
5
6
7
8
9
10
11
# 从 YAML 创建环境
conda env create -f environment.yml

# 指定环境名创建
conda env create -f environment.yml -n <custom-name>

# 从精确包列表安装
conda install --file spec-file.txt

# 更新现有环境
conda env update -f environment.yml --prune

environment.yml 最佳实践

1
2
3
4
5
6
7
8
9
10
name: my-project
channels:
- conda-forge
- defaults
dependencies:
- python=3.11
- pip>=23.0
# 通过 pip 安装 Conda 没有的包
- pip:
- some-package-only-on-pypi

Channel 配置与优化

国内镜像配置

1
2
3
4
5
6
7
8
9
10
11
12
# 清华镜像(推荐)
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/

# 阿里云镜像
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/main/
conda config --add channels https://mirrors.aliyun.com/anaconda/cloud/conda-forge/

# 中科大镜像
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/

Channel 优先级策略

1
2
3
4
5
6
7
8
# strict 模式(推荐):按顺序严格使用第一个匹配版本
conda config --set channel_priority strict

# flexible 模式:允许从高优先级 channel 安装依赖
conda config --set channel_priority flexible

# 禁用 channel 显示
conda config --set show_channel_urls yes

配置管理

1
2
3
4
5
6
7
8
9
10
11
# 查看完整配置
conda config --show

# 查看 channels 配置
conda config --show channels

# 删除所有自定义 channels
conda config --remove-key channels

# 重置为默认配置
conda config --add channels defaults

性能优化

依赖解析加速

1
2
3
4
5
6
# 使用 libmamba 求解器(显著加速)
conda install -n base conda-libmamba-solver
conda config --set solver libmamba

# 批量安装减少求解次数
conda install <pkg1> <pkg2> <pkg3> # 而非多次执行

缓存管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 清理未使用的包
conda clean -p

# 清理 tar 包
conda clean -t

# 清理索引缓存
conda clean -i

# 清理所有
conda clean --all

# 查看缓存占用
conda clean --dry-run

CI/CD 集成

GitHub Actions 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
name: Python CI
on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup Conda
uses: conda-incubator/setup-miniconda@v3
with:
activate-environment: myenv
environment-file: environment.yml
python-version: 3.11
auto-activate-base: false

- name: Run tests
shell: bash -l {0}
run: pytest

Docker 集成

1
2
3
4
5
6
7
8
9
FROM continuumio/miniconda3:latest

COPY environment.yml .
RUN conda env create -f environment.yml

# 激活环境并设置默认
SHELL ["conda", "run", "-n", "myenv", "/bin/bash", "-c"]
COPY . .
CMD ["conda", "run", "--no-capture-output", "-n", "myenv", "python", "app.py"]

版本控制与回滚

1
2
3
4
5
6
7
8
9
# 查看环境历史
conda list --revisions

# 回滚到指定版本
conda install --revision <rev-number>

# 清理旧版本历史(谨慎)
conda install conda-clean
conda clean -r

故障排查

常见问题解决

1
2
3
4
5
6
7
8
9
10
11
# 依赖解析失败时更新 conda
conda update conda

# 强制重新安装包
conda install --force-reinstall <package>

# 忽略已安装包更新
conda install --freeze-installed <package>

# 查看详细日志
conda install -v <package>

SSL 与代理

1
2
3
4
5
6
7
8
9
# 设置代理
conda config --set proxy_servers.http http://proxy:port
conda config --set proxy_servers.https https://proxy:port

# 禁用 SSL 验证(仅内网)
conda config --set ssl_verify false

# 指定 CA 证书
conda config --set ssl_verify /path/to/ca-bundle.crt

参考资源