Python Library 学习 - Conda
Python Library 学习 - Conda
1. package 操作
- 查看已安装的packages
conda list
- 安装
conda install $PACKAGE_NAME
conda install -n $ENV_NAME $PACKAGE_NAME
# example
conda install numpy
批量安装
conda install --yes --file requirements.txt -c conda-forge
- 指定源安装
conda install --channel $URL $PACKAGE_NAME
conda install -c $URL $PACKAGE_NAME
# "--channel" can be shortened as "-c"
# we can aslo use $CHANNEL_NAME replace $URL
# example
conda install -c conda-forge numpy
- 卸载
conda remove $PACKAGE_NAME
conda remove --name $ENVIRONMENT_NAME $PACKAGE_NAME
conda remove -n $ENVIRONMENT_NAME $PACKAGE_NAME
# "--name" can be shortened as "-n"
- 更新
更新 package 可以使用 update
命令或者 upgrade
命令
conda update $PACKAGE_NAME
conda update --name $ENVIRONMENT_NAME $PACKAGE_NAME
# 更新所有 packages
conda update --all
conda upgrade $PACKAGE_NAME
conda upgrade --name $ENVIRONMENT_NAME $PACKAGE_NAME
# 更新所有 packages
conda upgrade --all
- 导出环境中所有 package
conda list -myenv > requirements.txt
2. channel 操作
2.1. 常用命令
- 显示所有channel
conda config --show channels
- 查看当前channel 的状态和优先级
conda config --get channels
- 添加channel,并设置为最高优先级
conda config --add channels $CHANNEL_NAME
conda config --add channels $CHANNEL_URL
# example
conda config --add channels conda-forge
- conda 默认源
conda config --remove channels $CHANNEL_NAME
conda config --remove channels $CHANNEL_URL
- 设置channel的严格优先
conda config --set channel_priority strict
2.2. 国内常用的镜像 channel
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/
3. env 操作: 虚拟环境
- 查看虚拟环境
conda env list
conda info -e
- 创建虚拟环境
conda create --name $ENVIRONMENT_NAME python=X.X
conda create -n $ENVIRONMENT_NAME python=X.X
# "--name" can be shortened as "-n"
# example
conda create --name py38 python=3.8
conda create -n py38 python=3.8
- 激活虚拟环境
conda activate $ENVIRONMENT_NAME
- 退出虚拟环境,返回base环境
conda deactivate
- 删除虚拟环境
conda remove --name $ENVIRONMENT_NAME --all
4. solver
libmamba, 更快的计算器用于计算库之间的依赖
conda install -n $ENVIRONMENT_NAME conda-libmamba-solver
conda config --set solver libmamba
# 原始的 solver
conda config --set solver classic
5. 常见问题
1. 安装包不存在
conda install librosa
当出现以下错误提示时:
ackagesNotFoundError: The following packages are not available from current channels:
-
执行
anaconda search -t conda X
,显示可用的版本 -
选择适合自己的版本,执行:
conda install -c https://conda.anaconda.org/X
X
为所选择的安装的包如:
conda install -c https://conda.anaconda.org/conda-forge librosa
Reference
[1] Conda vs. pip vs. virtualenv commands, Conda documents
[2] A Faster Solver for Conda: Libmamba, 2022, site