Skip to content

Instantly share code, notes, and snippets.

@James-Leslie
Last active December 14, 2022 09:48
Show Gist options
  • Save James-Leslie/f00dee874592933f606d73be6891b2cd to your computer and use it in GitHub Desktop.
Save James-Leslie/f00dee874592933f606d73be6891b2cd to your computer and use it in GitHub Desktop.
Conda environments

Managing conda virtual environments

Conda documentation


1. Getting started

1.1. Create env

Create env with python 3.7 and pip

conda create --name whatwhale python=3.7 pip

1.2. Activate env

Activate by name

conda activate whatwhale

1.3. Deactivate env

conda deactivate

1.4. Removing an environment

Remove environment by name

# remove env
conda env remove --name whatwhale
# verify
conda env list

2. Managing packages

2.1. Viewing packages

List packages in active environment

conda list

List packages in environment using name

conda list --name whatwhale

2.2. Install packages

Install flask in active environment

conda install flask

Install flask in environment using name

conda install --name whatwhale flask

2.3. Using pip

It is recommended to only install packages with pip if conda install does not work.

Install pip into active environment

conda install pip
pip install <package-name>

3. Exporting environments

3.1. Save environment dependencies into a file

Produce a spec list file

conda list --explicit > requirements.txt

Create an environment.yml file

conda env export --from-history > environment.yml

3.2. Build an identical environment on the same or another machine:

Build a new environment from spec list file

conda create --name myenv -f requirements.txt

Recreate from environment.yml file

conda env create -f environment.yml

3.3. Update existing environment using a file

Update an existng environment from spec list file

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

Update an existng environment from environment.yml file

conda env update --name myenv --file environment.yml --prune
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment