以下总结均参考CSDN博客

一、获取帮助

1
2
3

conda -h # 获取帮助
conda env -h # 获取环境相关命令的帮助

二、更新

1
2
3
4
5
conda --version 					# 查看版本信息
conda update conda
conda update anaconda
conda update --all # 更新全部包
conda update xxx # 更新xxx文件包

三、创建环境

1
2
3
4
5
6
7
conda env list                    	# 显示所有的虚拟环境
source ~/conda/bin/activate # 激活base环境
conda create --name newname --clone oldname # 创建一个newname的新环境,里面的包与oldname相同
conda create -n xxxx python=3.9 # 创建python3.9的xxxx虚拟环境,创建新环境的时候最好指定python具体版本,不要创建空环境
conda activate xxxx # 激活虚拟环境,可用于不同环境互相切换
conda list # 查看当前环境中已经安装的包
conda deactivate # 退出当前环境

四、卸载

1
2
3
4
5
6
7
conda search package_name			# 可以在安装具体的某款包前查找conda库中是否有对应的版本
conda instal xxx # 安装xxx文件包
conda uninstall xxx # 卸载xxx文件包
conda env remove -n xxxx # 删除xxxx虚拟环境
# 下面的删除就是清理缓存
conda clean -p # 删除没有用的包,清理缓存
conda clean -t # 删除tar安装包

五、Conda数据源管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 显示目前conda的数据源有哪些
conda config --show channels
# 添加数据源:例如, 添加清华anaconda镜像:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
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/
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.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
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/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/pytorch/
# 上海交大镜像源
conda config --add channels https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.sjtug.sjtu.edu.cn/anaconda/cloud/conda-forge/
# linux 系统 将以上配置文件写在~/.condarc中 vim ~/.condarc
# 安装包时显示具体来源
conda config --set show_channel_urls yes
# 删除指定数据源
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
# 恢复默认数据源
conda config --remove-key channels

六、requirements相关

1
2
3
pip freeze > requirements.txt 		# 生成一个所需环境包的txt文件
# 使用conda和pip安装相应的包
while read requirement; do conda install --yes $requirement || pip install $requirement; done < requirements.txt