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.com.Creating an environment
Create a by opening Anaconda Prompt (Terminal on macOS/Linux) and following the steps for your use case below:- Creating a new environment
- Creating an environment from a `.yml` file
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 each <PACKAGE> with the name of your desired packages.
Replace <VERSION> with your desired version (optional).
Replace each <PACKAGE> with the name of your desired packages.
Replace <VERSION> with your desired version (optional).
Example environment creation command
Example version matching inputs
Example version matching inputs
Version matching utilizes MatchSpec Protocol. Below are some examples for specifying versions for packages when creating an environment. If you do not specify a package version, conda will attempt to install the latest version of the package from its available .
-
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.
It is best practice to install all of the packages that you need in your environment in a single command. This reduces the risk of dependency conflicts or broken environments.
Environment registration
Environment registration
When you create an environment, conda it to the
environments.txt file, which is located here:- Windows
- macOS/Linux
%USERPROFILE%\.conda\environments.txtconda --info envs uses the environments.txt file to show all existing environments on your machine, even those outside the base install directory.Environment locations
By default, conda creates environments in the following locations, depending on your operating system and how you’ve installed conda:| Operating System | Installer | Default Environment Location |
|---|---|---|
| Windows | Graphical installer (.exe) |
|
| macOS | Graphical installer (.pkg) |
|
| macOS | Command line installer (.sh) |
|
| Linux | Command line installer (.sh) |
|
--prefix command when creating an environment, or by updating your .condarc file to contain the envs_dirs: key.
Example .condarc configuration
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.
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:
-
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.
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 theconda-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.
environment.yml file, create one by running the following command:
conda-lock.default.yml file that you can export to share with others.
Sharing an environment
Sharing your environment with someone else allows them to use conda to recreate your environment on their machine. To share an environment and its software packages, you must export your environment’s configurations into a.yml file.
Exporting the environment configuration .yml file
If you already have an environment configuration
.yml file in your current directory, it will be overwritten during the export process.-
Activate the environment you want to export by running the following command:
Replace <ENV_NAME> with the name of the environment you want exported.
-
Export the environment by running the following command:
The
environment.ymlfile populates in your current working directory.
This operation includes both the environment’s conda and pip packages. -
Share the exported environment configuration
.ymlfile with another user.
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:When you deactivate an environment, conda returns to the previously activated environment.For example, if you run
conda activate my_env1, followed by conda activate my_env2, and then run conda deactivate, conda returns to the my_env1 environment.Removing an environment
To remove an environment, run the following command:Running
conda remove deletes and your environment.Troubleshooting
command not found: conda
Cause
The
command not found: conda error occurs when your command line interface (CLI) can’t find the conda command in order to use it. This might be because:- You don’t have conda properly initialized.
- You have set
auto_activate_basetofalse. - You’re using a shell that conda doesn’t support.
- Conda is not installed or the install was incomplete or corrupted.
These issues primarily occur on macOS/Linux computers. Anaconda Distribution and Miniconda installations on Windows include Anaconda Prompt, which opens with conda initialized by default.
Solution
Initialize conda in your shell
Initialize conda in your shell
If you recently installed Anaconda Distribution or Miniconda, make sure you closed and reopened your CLI to make conda’s initialization take effect.You can also initialize conda directly from its
bin directory:Replace <PATH_TO_CONDA> with a path to your conda installation.
Set auto_activate_base to true
Set auto_activate_base to true
To see the value for If your terminal returns If you have
auto_activate_base, run the following command:false, this means that conda is not automatically activating your base environment when you start a new shell. This behavior emulates your system Python, and some users prefer to have their conda environment be inactive until they need it. However, this is not conda’s default behavior after installation.To change the value of auto_activate_base to true, run the following command:auto_activate_base set as false, the conda command will still be available as a shell function, but your base environment will not be active when a new shell is started. To activate your base environment, run conda activate.Use a shell that conda supports
Use a shell that conda supports
For information on which shells conda supports, see Conda activate in the official conda documentation.
Verify your installation of conda
Verify your installation of conda
If you have tried to initialize conda in your shell but it didn’t work, try uninstalling and reinstalling Anaconda Distribution or Miniconda.
DirectoryNotACondaEnvironmentError: The target directory exists, but it is not a conda environment
Cause
This error means that you don’t have a conda environment currently active. You might have used
conda deactivate while in your base environment and deactivated conda.Solution
Activate your
base environment or another environment:Replace <ENV_NAME> with the name of the environment.
PackagesNotFoundError: The following packages are not available from current channels
Cause
The
PackagesNotFoundError occurs when conda cannot find the requested packages in any of your currently configured channels in order to install or update it. This might be because:- The package name is misspelled.
- The package doesn’t exist in the channels included in your conda configuration file (
.condarc). - The package is not available for your operating system or architecture.
Solution
Verify your command syntax
Verify your command syntax
Verify that your package name is spelled correctly and that your command contains no syntax errors. For more information about command syntax, see Using conda install.
Install from the package's details page
Install from the package's details page
If you’re unsure what the correct command to install the package is, you can find it on the package’s details page.Search Anaconda.org or your Anaconda Platform channels for your desired package, and view its details. Each package’s details page provides a copyable command to install the package from that channel. Run that command to install the package in your currently active environment.
Add the missing channel to your .condarc file
Add the missing channel to your .condarc file
Add the missing channel to your .condarc file. However, keep in mind that any channels listed in the
channels: section of your .condarc file will be searched by conda every time you install a new package.Verify the package is compatible with your system architecture
Verify the package is compatible with your system architecture
Check your system architecture to determine if the package is compatible. This information is found under Conda restricts searches to packages that are compatible with your system architecture. If the package you’re looking for is not available for your system architecture, it is not listed in conda’s package search results.
platform: in your conda info output.