Environments#
Environments in conda are self-contained, isolated spaces where you can install specific versions of software packages, including dependencies, libraries, and Python versions. This isolation helps avoid conflicts between package versions and ensures that your projects have the exact libraries and tools they need.
Why should I create a new environment?#
There are several reasons you might want to create a new environment:
Isolation of dependencies - Environments isolate software and their dependencies from the rest of the software installed on your machine. This means you can have both Python 3.9 and Python 3.10 installed on your machine and use both versions without encountering issues.
Reproducibility - By creating an environment for each project, you can ensure that your code runs consistently across different machines. Lock your environment to ensure that it remains reproducible indefinitely, then share the environment configuration to allow others to replicate your setup.
Ease of management - Conda provides tools to easily create, manage, and delete environments. You can quickly switch between environments, making it simple to manage multiple projects with different requirements.
Testing and development - Environments are perfect for testing new packages or libraries without affecting your stable development setups. You can experiment freely and remove the environment if things don’t work out, without impacting your other projects.
Why shouldn’t I work in the base environment?#
When first installing and using conda, you probably saw references to something called base
or a “base environment”. This environment is where conda itself is installed, and should only be used for installing anaconda, conda, and conda-related packages, such as anaconda-client
or conda-build
.
For your projects, however, Anaconda strongly recommends creating new environments to work in. This protects your base environment from breaking due to complex dependency conflicts and allows you to easily manage and reproduce your environment on other machines.
Working with environments#
For convenience, the most common actions users take when managing environments are detailed here. For a full list of actions and more comprehensive guide, see Manage environments in the official conda documentation. Alternatively, follow along with our Getting started with conda environments tutorial on Anaconda Cloud.
Tip
If you prefer to create and manage your environments via our graphical interface, Navigator, see Managing environments.
Creating an environment#
Create a conda environment by opening Anaconda Prompt (Terminal on macOS/Linux) and following the steps for your use case below:
The following command creates a new environment and downloads the specified packages and their dependencies:
# Replace <ENV_NAME> with a name for your environment
# Replace <PACKAGE> with your desired package
# Replace <VERSION> with your desired version (optional)
conda create --name <ENV_NAME> <PACKAGE>=<VERSION>
Example:
conda create --name myenv python=3.11 beautifulsoup4 docutils jinja2=3.1.4 wheel
Example version matching inputs
Version matching utilizes Matchspec Protocol. Below are some examples for specifying versions for packages when creating an environment. For more information, see MatchSpec in the official conda documentation. If you do not specify a package version, conda will attempt to install the latest version of the package from its available channels.
python=3.12.1
- Matches the package with the exact name and version specified.
python>=3.11
- Matches any version of the package that is greater than or equal to the version specified.
python<=3.12
- Matches any version of the package that is less than or equal to the version specified.
python>3.10,<3.12
- Matches any version of the package between the specified versions, but excludes the specified versions.
python>=3.10,<=3.12
- Matches any version of the package between the specified versions, including the specified versions.
python[version='3.12.*']
- Matches any version of the python package that starts with 3.12.
If someone has shared an environment with you, or you have exported a .yml
file to recreate your environment on a new machine, run the following command from the directory containing the .yml
file:
conda env create --file environment.yml
Note
The first line of the file sets the environment’s name. For more information, see Creating an environment file manually.
Activating an environment#
Because environments are isolated spaces, you can only work with one at a time. Selecting an environment to work with is called activating it.
Activate an environment by running the following command:
# Replace <ENV_NAME> with the name of the environment you want to activate conda activate <ENV_NAME>
Switching between environments#
When you’re ready to switch between projects, simply activate the environment of your other project. Activating a different environment will deactivate your current one.
(Optional) View a list of all your environments by running the following command:
conda info --envs
To switch to a different environment, activate it by running the following command:
# Replace <ENV_NAME> with the name of the environment you want to switch to conda activate <ENV_NAME>
Locking an environment#
The most reliable way to ensure your project remains reproducible indefinitely is to “lock” its environment. Locking an environment creates a fully specified environment, one that has all packages used in the project and their dependencies configured to a specific version. This ensures that your project will be reproduced exactly as it was initially configured, because there will never be an unexpected update or change if new package dependencies are released.
Locking your project requires the conda-project
package to be installed in the environment you want to lock. Install the package by running the following commands:
# Replace <ENV> with the environment you want to lock
conda activate <ENV>
conda install conda-project
If your project doesn’t contain an environment.yml
file, create one by running the following command:
conda-project init
You can then lock your project’s environment by running the following command:
conda-project lock
Locking your project produces a conda-lock.default.yml
file that you can export to share with others.
Deactivating an environment#
It is best practice to deactivate your environment when you are finished working in it.
To deactivate your active environment, run the following command:
conda deactivate