Skip to main content

Conda Environment Management

Creating and Managing Conda Environments

Creating an Environment

To create a new environment:

conda create --name <my-env>

Replace <my-env> with the desired name of your environment.

To create an environment with a specific version of Python:

conda create -n myenv python=3.9

To create an environment with a specific package:

conda create -n myenv scipy

or

conda create -n myenv python
conda install -n myenv scipy

To create an environment with a specific version of a package:

conda create -n myenv scipy=0.17.3

or

conda create -n myenv python
conda install -n myenv scipy=0.17.3

To create an environment with a specific version of Python and multiple packages:

conda create -n myenv python=3.9 scipy=0.17.3 astroid babel

Tip: Install all desired programs at once to avoid dependency conflicts. To automatically install default programs (e.g., pip) in every new environment, add them to the create_default_packages section of your .condarc file. To prevent default packages in a specific environment, use:

conda create --no-default-packages -n myenv python

Creating an Environment from an Environment File

To create an environment from an environment.yml file:

conda env create -f environment.yml

Activate the new environment:

conda activate myenv

Verify the environment:

conda env list

or

conda info --envs

Specifying a Location for an Environment

To specify a custom location for an environment:

conda create --prefix ./envs jupyterlab=3.2 matplotlib=3.5 numpy=1.21

Activate the environment:

conda activate ./envs

Modify the env_prompt setting in your .condarc file to simplify long environment paths:

conda config --set env_prompt '({name})'

Updating an Environment

To update the environment based on changes in the environment.yml file:

conda env update --file environment.yml --prune

The --prune option removes dependencies no longer required.

Cloning an Environment

To clone an existing environment:

conda create --name myclone --clone myenv

Verify the clone:

conda info --envs

Building Identical Conda Environments

Generate a spec list:

conda list --explicit > spec-file.txt

Create an environment from the spec file:

conda create --name myenv --file spec-file.txt

Install packages from the spec file into an existing environment:

conda install --name myenv --file spec-file.txt

Activating and Deactivating Environments

To activate an environment:

conda activate myenv

To deactivate an environment:

conda deactivate

Nested Activation

To stack environments:

conda activate --stack myenv

Set auto-stacking:

conda config --set auto_stack 1

Environment Variables

Set environment variables:

conda env config vars set my_var=value
conda activate myenv

Unset environment variables:

conda env config vars unset my_var -n test-env

Environment variables can also be set in the environment.yml file:

name: env-name
channels:
- conda-forge
- defaults
dependencies:
- python=3.7
- codecov
variables:
VAR1: valueA
VAR2: valueB

Sharing and Exporting Environments

To export an environment:

conda env export > environment.yml

For cross-platform compatibility:

conda env export --from-history

Restoring and Removing Environments

To restore an environment to a previous revision:

conda install --rev REVNUM

To remove an environment:

conda remove --name myenv --all

or

conda env remove --name myenv

Viewing and Managing Environments

List all environments:

conda info --envs

or

conda env list

List packages in an environment:

conda list -n myenv

To check for a specific package:

conda list -n myenv scipy

Using Pip in Conda Environments

Install pip in an environment:

conda install -n myenv pip
conda activate myenv
pip <pip_subcommand>

Best practices when combining pip and conda:

  • Use pip only after conda has installed as many packages as possible.
  • Use isolated environments to avoid conflicts.
  • Avoid running pip in the root environment.
  • Store requirements in text files and use --file for conda and -r for pip.

Setting Environment Variables in Scripts

Windows:

mkdir %CONDA_PREFIX%\etc\conda\activate.d
mkdir %CONDA_PREFIX%\etc\conda\deactivate.d
type NUL > %CONDA_PREFIX%\etc\conda\activate.d\env_vars.bat
type NUL > %CONDA_PREFIX%\etc\conda\deactivate.d\env_vars.bat

# Activate script
set MY_KEY='secret-key-value'
set MY_FILE=C:\path\to\my\file

# Deactivate script
set MY_KEY=
set MY_FILE=

macOS and Linux:

mkdir -p $CONDA_PREFIX/etc/conda/activate.d
mkdir -p $CONDA_PREFIX/etc/conda/deactivate.d
touch $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
touch $CONDA_PREFIX/etc/conda/deactivate.d/env_vars.sh

# Activate script
export MY_KEY='secret-key-value'
export MY_FILE=/path/to/my/file/

# Deactivate script
unset MY_KEY
unset MY_FILE