📙Conda Note

Conda cheat sheet
Conda documentation

Conda Channels

Conda installs packages from default channel if no channel is specified.

Use conda config --show channels to see the current channel list.

Subject to the GFW, downloading process may be very slow in mainland of China.

The once for all solution is breaking the wall or leaving the mainland, both hard to achieve.

You can circumvent the GFW through proxy or use domestic channel as alternative. I prefer the former.

Setup Proxy for Conda

Suppose you have a local socks5 proxy listening port 1080, simply modified the .condarc :

proxy_servers:
  http: socks5://127.0.0.1:1080
  https: socks5://127.0.0.1:1080

Add Channel List

Two ways to add channel list:

  1. Override the config file:

Create .condarc in %UserProfile%/.conda, follow the YAML syntax, override the channel list configuration like:

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - defaults

Channels are organized from highest to lowest priority.

  1. Manager channels from command prompt:
REM "Add to the top of the channel list"
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/

OR

REM "Add to the bottom of the channel list"
conda config --append channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --append channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

Specify Channels When Installing Packages

Use -c or --channel flag to add additional channel to search when installing:

conda install numpy --channel https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/

And use --override-channels flag to skip the channels list in .condarc

conda install numpy --override-channels --channel https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/

Conda Packages

REM "Searh package and display the detailed information"
conda search PKGNAME --info
REM "Specify version"
conda install PKGNAME==3.14
REM "Specify channel"
conda install --channel conda-forge
conda install -c conda-forge
REM "Specify environment"
conda install PKGNAME --name ENVNAME
conda install PKGNAME -n ENVNAME
REM "Install from local directory"
conda install PATH/TO/PKGNAME.tar.bz2 --offline
conda update PKGNAME --name ENVNAME
conda remove PKGNAME --name ENVNAME

Conda Environment

Conda allows you to create environments containing different packages and even different python version that will not interact with other environments.

Conda will install package to base environment as default if no environment is specified when installing.

Here are some convenient command to manager conda environment:

  • List available conda environments(name & path):
REM "Current environment is highlighted with an asterisk(*)"
conda info --envs

Or just go to %UserProfile%/.conda/environments.txt

  • Create environment
REM "Create environment with specific python version"
conda create --name ENVNAME python==VERSION
REM "Create environment to specific path"
conda create --prefix PATH/TO/ENVNAME
REM "Create environment from .yaml file"
conda env create --file PATH/TO/environment.yaml
REM "Create environment from .txt file"
conda create --name ENVNAME --file PATH/TO/spec-file.txt
REM "Create environment from existing environment"
conda env create --name NEWNAME --clone OLDNAME
  • Activate environment
conda activate ENVNAME
  • Deactivate environment
conda deactive
  • List packages in specific environment
conda list --name ENVNAME
  • Using pip in an environment
conda install --name ENVNAME pip
conda activate myenv
pip <pip_subcommand>
REM "example: pip install PKGNAME -f LINK/LOCAL_PATH"
  • Export environment
REM "Export to .yaml file"
conda activate ENVNAME
conda env export > PATH/TO/environment.yaml
REM "Export spec list as txt file"
conda list --name ENVNAME --explicit > PATH/TO/spec-file.txt
  • Remove environment
conda remove --name ENVNAME --all