conda 常用命令

1. conda的安装

#在官网https://www.anaconda.com下载anaconda3的安装包(根据系统选择版本)
wget https://repo.anaconda.com/archive/Anaconda3-5.3.1-Linux-x86_64.sh 

#运行.sh
bash https://repo.anaconda.com/archive/Anaconda3-5.3.1-Linux-x86_64.sh 

#检查conda初始化
which conda
which python

可能遇到的问题:
 1)glibc版本依赖性:如遇错误提示version 'GLIBC_2.17' not found,或者The version of GLIBC is no longer supported等。
  >>解决方案:改换成低版本的anaconda(或者更新GLIBC,不建议,操作麻烦且容易出错)
 2)初始化失败:如which conda后没有自己安装时的prefix
  >>解决方案:通过vi ~/.bashrc vi ~/.bash_profile source ~/.bashrc ~/.bash_profile设置环境变量(参考:https://www.bilibili.com/video/BV1hC4y187Au)
 3)其他可参考官网文档

2. 创建conda虚拟环境

#创建虚拟环境
conda create -n <env_name> <环境>=<版本号> 
#<>内根据自己需要更改,如我要创建一个叫做myenv的python虚拟环境
conda create -n myenv python=3.7.0

#查看环境
conda info --e

#激活虚拟环境
conda activate myenv

#退出虚拟环境
conda deactivate

可能遇到的问题:
如果在创建虚拟环境时,遇到错误提示

Solving environment: failed
PackagesNotFoundError: The following packages are not available from current channels:
- python=3.10.6

考虑使用添加channel的方式,步骤如下:

  1. 在anaconda官网https://anaconda.org找到该库的channel,如图所示,提示channel为conda-forge。注意,找到的库最好与电脑版本对应(如osx-arm64)
  2. 添加对应的channel,如:
conda config --add channels conda-forge
conda create -n myenvs python=3.10.6
  1. 此时创建环境成功。

3. conda安装软件/库如netcdf4

#netcdf4 
conda install -c conda-forge netcdf4  #可通过conda search netcdf4查找通道及版本

4. conda的配置

查看conda的配置

#查看conda的配置信息
conda config --show  

#查看当前使用源
conda config --show-sources  

# ==> ~/.condarc <==可修改channels
vim ~/.condarc  
# condarc默认会显示:channels:
##- defaults

conda自定义channels
方法1:修改~/.condarc,如上
方法2:命令行(参考:https://www.cnblogs.com/hightech/p/12758770.html

#添加channels
conda config --add channels  

#从channel中安装包时显示channel的url,便可知道包的安装来源
conda config --set show_channel_urls yes  

#删除channels
conda config --remove channels  
posted @ 2022-03-09 21:18  dan-chen  阅读(435)  评论(0编辑  收藏  举报