Table of Contents

dummy

tasks

Remove an environment

To do it with no questions asked

conda env remove --name foo --yes

Ask for confirmation

conda env remove --name foo

If the environment was installed in a non standard location

conda env remove -p foo

where foo is the path of the environment.

tags | delete

related commands:

conda info --envs
conda remove --help

update environment

conda env update -f environment.yml

where environment.yml is the path of the environment file

Ref:- https://conda.io/docs/commands/env/conda-env-update.html

update conda

conda update -n base -c defaults conda --repodata-fn=repodata.json
conda update -n <env_name> -c defaults --yes conda

For example

conda update -n base -c defaults --yes conda

To specify a particular version number, X.Y.Z (ex:- 4.5.4)

conda update -n base -c defaults --yes conda=X.Y.Z

To downgrade to a specific version

conda config --set allow_conda_downgrades true
conda install -n base -c defaults conda=4.6.14

tags | upgrade conda

Ref:-

command cheat sheet

purpose command
List all packages in the current environment conda list
List the version of a package in all the conda environments
for env in `conda info --envs | stuff.py | cut -f 1 -d ' '`
do
  echo $env
  conda list <package_name> -n $env
  echo
done

where stuff.py is from https://github.com/KamarajuKusumanchi/rutils/blob/master/python3/stuff.py

For example

for env in `conda info --envs | stuff.py | cut -f 1 -d ' '`
do
  echo $env
  conda list agate -n $env
  echo
done 

gives something like

...
py310
# packages in environment at <munged_path>:
#
# Name                    Version                   Build  Channel
agate                     1.7.1           py310haa95532_0    defaults
agate-dbf                 0.2.2                      py_0    defaults
agate-excel               0.2.5              pyhd3eb1b0_0    defaults
agate-sql                 0.5.8              pyhd3eb1b0_0    defaults

py311
# packages in environment at <munged_path>:
#
# Name                    Version                   Build  Channel
agate                     1.7.1           py311haa95532_0    defaults
agate-dbf                 0.2.2                      py_0    defaults
agate-excel               0.2.5              pyhd3eb1b0_0    defaults
agate-sql                 0.5.8              pyhd3eb1b0_0    defaults
...
Show the channel urls conda list –show-channel-urls
Search packages available in Anaconda conda search -f <package_name>

find the package version

tags | check package version, list packages in environment, show the package version, conda list package exact match

To find the version of a package installed

conda list package_name -n env_name

For example

% conda list pandas -n py39
# packages in environment at /opt/rajulocal/miniconda3/envs/py39:
#
# Name                    Version                   Build  Channel
pandas                    1.3.4            py39h8c16a72_0  
pandas-datareader         0.10.0             pyhd3eb1b0_0  
pandas-market-calendars   3.2                      pypi_0    pypi

If you just want to see the version of pandas but not that of packages that have pandas in their name (ex:- pandas-datareader), use regex. For example

% conda list ^pandas$ -n py39
# packages in environment at /opt/rajulocal/miniconda3/envs/py39:
#
# Name                    Version                   Build  Channel
pandas                    1.3.4            py39h8c16a72_0

You can also use this technique to check if a package is installed or not.

To check the versions of multiple packages

conda list -n env_name | grep -E "pkgA|pkgB"

For example

% conda list -n py39 | grep -E "pandas|numpy"
numpy                     1.21.2           py39hd8d4704_0  
numpy-base                1.21.2           py39h2b8c604_0  
pandas                    1.3.4            py39h8c16a72_0  
pandas-datareader         0.10.0             pyhd3eb1b0_0  
pandas-market-calendars   3.2                      pypi_0    pypi

Ref:- https://conda.io/docs/commands/conda-list.html

what version of a package is installed in all the environments?

conda info --envs | grep -v "^#" | cut -f 1 -d ' ' | xargs -I{} sh -c "conda list foo -n {}"

For example, to check what versions of pandas is installed in all the environments, do

conda info --envs | grep -v "^#" | cut -f 1 -d ' ' | xargs -I{} sh -c "conda list ^pandas$ -n {}"

Note:- We have to use ^pandas$ instead of pandas since the latter will show versions of other packages that have pandas in their name (ex:- pandas-datareader)

list available package versions

List all packages in the current environment

$ conda list
# packages in environment at C:\ProgramData\Continuum\Anaconda\envs\numpy_issue:
#
# Name                    Version                   Build  Channel
certifi                   2019.3.9                 py27_0
numpy                     1.16.4                   pypi_0    pypi
pip                       19.1.1                   py27_0
python                    2.7.16               hcb6e200_0
setuptools                41.0.1                   py27_0
sqlite                    3.28.0               h0c8e037_0
vc                        9                    h7299396_1
vs2008_runtime            9.00.30729.1         hfaea7d5_1
wheel                     0.33.4                   py27_0
wincertstore              0.2              py27hf04cefb_0

Show the channel urls

$ conda list --show-channel-urls
# packages in environment at C:\ProgramData\Continuum\Anaconda\envs\numpy_issue:
#
# Name                    Version                   Build  Channel
certifi                   2019.3.9                 py27_0    defaults
numpy                     1.16.4                   pypi_0    pypi
pip                       19.1.1                   py27_0    defaults
python                    2.7.16               hcb6e200_0    defaults
setuptools                41.0.1                   py27_0    defaults
sqlite                    3.28.0               h0c8e037_0    defaults
vc                        9                    h7299396_1    defaults
vs2008_runtime            9.00.30729.1         hfaea7d5_1    defaults
wheel                     0.33.4                   py27_0    defaults
wincertstore              0.2              py27hf04cefb_0    defaults

Related:

reduce disk space usage

conda clean --dry-run --all
conda clean --all

Ref:-

where is the package cache

conda info

tags | conda cache directory

Related commands:

conda clean --all --dry-run
conda clean --packages --dry-run

where is my condarc file?

$ conda info | grep "user config file"
       user config file : C:\Users\raju\.condarc

which solver am I using?

Use

conda config --show solver

or

conda config --show-sources

Sample output:

$ conda config --show solver
solver: classic
$ conda config --show solver
solver: libmamba
$conda config --show-sources
==> C:\Users\root\.condarc <==
solver: libmamba

==> cmd_line <==
debug: False
json: False

See also:-

when was a conda environment created?

conda env list | grep -v '^#' | perl -lane 'print $F[-1]' | xargs ls -lrt1d

For example

 % conda env list | grep -v '^#' | perl -lane 'print $F[-1]' | xargs ls -lrt1d
drwxr-xr-x 10 rajulocal rajulocal 4096 Apr  4  2020 /opt/rajulocal/miniconda3/envs/rutils
drwxr-xr-x 23 rajulocal rajulocal 4096 Nov  8  2021 /opt/rajulocal/miniconda3/envs/py39
drwxr-xr-x 13 rajulocal rajulocal 4096 Jun  6  2022 /opt/rajulocal/miniconda3/envs/play_ground
drwxr-xr-x 27 rajulocal rajulocal 4096 Feb 28  2023 /opt/rajulocal/miniconda3/envs/py310
drwxr-xr-x 13 rajulocal rajulocal 4096 May 20  2023 /opt/rajulocal/miniconda3/envs/test_yfinance
drwxr-xr-x 18 rajulocal rajulocal 4096 Jun 30 00:45 /opt/rajulocal/miniconda3
drwxr-xr-x 27 rajulocal rajulocal 4096 Jul 11 01:07 /opt/rajulocal/miniconda3/envs/py311
drwxr-xr-x 24 rajulocal rajulocal 4096 Nov 12 04:44 /opt/rajulocal/miniconda3/envs/market_data_processor

Ref: https://stackoverflow.com/questions/69109316/how-can-i-see-the-creation-dates-for-my-conda-environments

activate conda environment

tags | activate conda environment through shell script

source activate vs. conda activate

For conda >= 4.4.0, use “conda activate” instead of “source activate”. The former is faster and the command remains same across OSes.

From https://github.com/conda/conda/blob/master/CHANGELOG.md#440-2017-12-20 → “New Feature Highlights” section

conda activate: The logic and mechanisms underlying environment activation have been reworked. With conda 4.4, conda activate and conda deactivate are now the preferred commands for activating and deactivating environments. You'll find they are much more snappy than the source activate and source deactivate commands from previous conda versions. The conda activate command also has advantages of (1) being universal across all OSes, shells, and platforms, and (2) not having path collisions with scripts from other packages like python virtualenv's activate script.

See also: conda activate, source activate

conda activate

# As described in https://github.com/conda/conda/issues/8186
# The conda activate command is failing with
# conda.sh: line 55: PS1: unbound variable
# Until the issue is fixed, set +eu before calling "source activate"
# and set -eu afterwards
set +eux
# If the conda.sh is not sourced, then 'conda activate' is giving
# CommandNotFoundError: Your shell has not been properly configured
# to use 'conda activate'.
source "$(dirname $(which conda))/../etc/profile.d/conda.sh"
conda activate $ENV_NAME
set -eux

tested with conda 4.8.3 on a machine running RHEL 7.6

See also: source activate vs. conda activate, source activate

source activate

# As described in https://github.com/conda/conda/issues/8186
# The conda activate command is failing with
# conda.sh: line 55: PS1: unbound variable
# Until the issue is fixed, set +eu before calling "source activate"
# and set -eu afterwards
set +eux
source activate $ENV_NAME
set -eux

tested with conda 4.8.3 on a machine running RHEL 7.6

See also: source activate vs. conda activate, conda activate

Sample environment files

sample environment file

name: sample_py36
channels:
  - conda-forge
  - defaults
dependencies:
  - python=3.6.8
  - pandas=0.24.1
  - pip:
    - pywinauto
    - fastavro==0.21.18

environment file to install pip package from a local repository

One way to specify a wheel file stored in a local directory as a pip dependency in a conda environment file is to do something like

name: test_ibapi
channels:
  - defaults
dependencies:
  - python=3.11
  - pip
  - pip:
    - file:///home/rajulocal/software/dist/ibapi-10.22.1-py3-none-any.whl

but this will not be portable when you move to a different machine since the wheel file path (/home/rajulocal/software/dist/ibapi-10.22.1-py3-none-any.whl) is hard coded. A better approach is to run

 % pip config set global.find-links file:///home/rajulocal/software/dist

which will modify pip.conf as

 % cat ~/.config/pip/pip.conf 
[global]
find-links = file:///home/rajulocal/software/dist

and then specify ibapi as a normal pip dependency.

name: test_ibapi
channels:
  - defaults
dependencies:
  - python=3.11
  - pip
  - pip:
    - ibapi

Ref:-

tags | --find-links, pip config local packages, how to specify local repo location for pip packages, conda install pip package from a “local directory”

specify package version with git+https

name: test_chessli
channels:
  - defaults
dependencies:
  - python=3.11
  - pandas=1.5.3
  - pip
  - pip:
    - chessli
    - git+https://github.com/lervag/apy.git@v0.6.0#egg=apy

Ref:-

tags | pip “git+https” release, conda “git+https” release, conda git depend on a “specific release”, specify package version with git+https in conda environment file

dummy

create an environment on the fly

Sample commands:

conda create -n <myenv> python ipython prompt_toolkit=2 jupyter pandas numpy scipy scikit-learn matplotlib

conda create -n <myenv> python=3.9 pandas pandas-datareader

commands I came across

$ conda config --add pinned_packages defaults::conda

To revert

$ conda config --remove-key pinned_packages

What is the difference between conda, pip, and virtualenv?

conda release numbers

See also:

twsapi conda environment file

tags | conda envrionment install package from zip file, conda install zip package, install zip file