Skip to content

文章标签

← 返回首页地图

Conda 虚拟环境常用命令与排错 ​

配 Python 项目最怕不同项目的库版本互相打架,用 Conda 给每个项目单独开环境最省心。这里记一下平时高频用到的命令和几个踩过的坑。

打开 Conda Prompt 踩坑 ​

如果双击 Anaconda Navigator 无反应、连图标都不弹出,通常是配置文件字符编码冲突(常见 GBK 与 UTF-8 混用)。可以在终端直接运行 anaconda-navigator,根据报错定位到对应文件把编码改成 UTF-8 即可。

如何创建你的第一个虚拟环境 ​

sh
# 创建虚拟环境
conda create --name env_name python=3.6 
# 激活虚拟环境
activate  env_name # 激活/使用/进入某个虚拟环境
# 安装python包
conda  install  xxx=版本号  # 指定版本号

指定虚拟环境 ​

sh
activate  env_name # 激活/使用/进入某个虚拟环境
sh
deactivate # 退出当前环境
sh
conda  create  --name  new_env_name  --clone  old_env_name # 复制某个虚拟环境
sh
conda  remove  --name  env_name  --all # 删除某个环境

查看全局环境 ​

sh
conda  info  --envs   # 查看当前所有环境
conda  env  list      # 查看当前所有环境

更新当前环境的python ​

sh
conda search python         # 检查可用的python版本
conda install python=x.x.x  # 更新到希望的版本
conda update python         # 更新到最新的 Python 版本

查看当前虚拟环境下的所有安装包 ​

sh
conda  list  需进入该虚拟环境
conda  list  -n  env_name

安装或卸载包(进入虚拟环境之后) ​

sh
conda  install  requests
conda  install  xx=版本号  # 指定版本号
conda  install  xxx -i 源名称或链接 # 指定下载源
conda  uninstall  xxx

分享虚拟环境 ​

sh
conda env export > environment.yml  # 导出当前虚拟环境
conda env create -f environment.yml  # 创建保存的虚拟环境

源服务器管理 ​

conda当前的源设置在$HOME/.condarc中,可通过文本查看器查看或者使用命令>conda config --show-sources查看。

sh
conda config --show-sources #查看当前使用源
conda config --remove channels 源名称或链接 #删除指定源
conda config --add channels 源名称或链接 #添加指定源

例如:

sh
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/

常用国内 Pip 源

  • 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple/
  • 阿里云:https://mirrors.aliyun.com/pypi/simple/
  • 中国科学技术大学:https://pypi.mirrors.ustc.edu.cn/simple/
  • 豆瓣:https://pypi.douban.com/simple/

升级 ​

升级Anaconda需先升级conda

sh
conda  update  conda
conda  update  anaconda

卸载 ​

sh
rm  -rf  anaconda

批量导出虚拟环境中的所有组件 ​

sh
conda list -e > requirements.txt  # 导出
conda install --yes --file requirements.txt  # 安装

pip批量导出环境中的所有组件 ​

sh
pip freeze > requirements.txt
pip install -r requirements.txt

国内下载资源需要科学上网,因此我们需要配置国内镜像源 ​

添加清华源 ​

sh
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.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

添加阿里云镜像源 ​

sh
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/free/ 
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/main/

添加中科大源 ​

sh
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
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/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/

(可选)设置搜索时显示通道地址 ​

sh
conda config --set show_channel_urls yes

安装 OpenCV 环境 ​

sh
# 1. 创建并激活环境
conda create --name opencv python=3.8
conda activate opencv

# 2. 安装 OpenCV(主库与 contrib 扩展库版本号必须一致)
pip install opencv-python==3.4.1.15
pip install opencv-contrib-python==3.4.1.15

# 3. 顺手装上常用的 Jupyter 和 Matplotlib
pip install jupyter matplotlib

# 4. 启动 Jupyter Notebook
jupyter notebook

安装 PyTorch 踩坑检查 ​

装完 PyTorch 后,建议第一时间跑一下这段代码确认是不是装对了 GPU 版本:

py
import torch

print(torch.__version__)          # 如果显示 2.6.0+cpu 说明装成 CPU 版了
print(torch.cuda.is_available())  # False 说明没有启用 CUDA

如果输出 +cpu 且 is_available() 为 False,多半是被第三方 Conda 镜像源拉成了默认 CPU 版。建议先卸载再用 PyTorch 官网 提供的 pip --index-url 命令安装对应 CUDA 版本。

记录实践,整理思路。

慢慢读,随手记。