Environment Management Intermediate

Conda environments are isolated spaces where you can install specific versions of Python and packages without affecting other projects. This is the core feature that makes Conda indispensable for data science workflows.

Creating Environments

Create a new environment with a specific Python version:

Terminal
# Create environment with a specific Python version
$ conda create -n myenv python=3.11

# Create with packages pre-installed
$ conda create -n datascience python=3.11 numpy pandas matplotlib

# Create with a specific Python and channel
$ conda create -n mlenv python=3.11 -c conda-forge

# Create in a specific directory (not default location)
$ conda create --prefix ./envs/myproject python=3.11

Activating and Deactivating

Terminal
# Activate an environment
$ conda activate myenv
(myenv) $  # Prompt changes to show active env

# Deactivate (return to base)
$ conda deactivate

# Switch directly to another environment
$ conda activate other_env
Always activate before installing: Make sure you activate your target environment before running conda install. Otherwise, packages get installed into the base environment.

Listing Environments

Terminal
# List all environments
$ conda env list
# or equivalently
$ conda info --envs

base                  *  /home/user/miniconda3
myenv                    /home/user/miniconda3/envs/myenv
datascience              /home/user/miniconda3/envs/datascience

Removing Environments

Terminal
# Remove an environment and all its packages
$ conda env remove -n myenv

# Or using conda remove with --all flag
$ conda remove -n myenv --all

Cloning Environments

Create an exact copy of an existing environment:

Terminal
# Clone an existing environment
$ conda create --name myenv_copy --clone myenv

Exporting and Sharing Environments

Export your environment to a YAML file so others can recreate it:

Terminal
# Export the active environment (includes all dependencies)
$ conda env export > environment.yml

# Export only explicitly installed packages (cross-platform friendly)
$ conda env export --from-history > environment.yml

Here is what an environment.yml file looks like:

YAML
name: datascience
channels:
  - conda-forge
  - defaults
dependencies:
  - python=3.11
  - numpy=1.24
  - pandas=2.0
  - scikit-learn=1.3
  - matplotlib=3.7
  - jupyter=1.0
  - pip:
    - some-pip-only-package==1.2.3

Creating from YAML

Terminal
# Create environment from a YAML file
$ conda env create -f environment.yml

# Update an existing environment from YAML
$ conda env update -f environment.yml --prune

Stacking Environments

You can activate an environment on top of another using --stack:

Terminal
# Stack environments (packages from both are available)
$ conda activate --stack another_env
Environment Locations: By default, environments are stored in ~/miniconda3/envs/ (or ~/anaconda3/envs/). You can create environments in custom locations using --prefix instead of -n.

Environment Workflow Summary

Task Command
Create environment conda create -n myenv python=3.11
Activate conda activate myenv
Deactivate conda deactivate
List environments conda env list
Remove environment conda env remove -n myenv
Clone environment conda create --clone myenv -n copy
Export to YAML conda env export > environment.yml
Create from YAML conda env create -f environment.yml

Practice Time

Try creating an environment, installing a few packages, exporting it to YAML, then recreating it from the YAML file. Next, learn how to manage packages inside your environments.

Next: Package Management →