Managing environments¶
Contents
With conda, you can create, export, list, remove, and update environments that have different versions of Python and/or packages installed in them. Switching or moving between environments is called activating the environment. You can even share an environment file with a coworker.
Anytime you wish to see the full documentation for any command, type the command followed by --help
. For example, to learn about the conda environment command:
conda env --help
The same help that is available in conda is also available online from our command reference documentation.
Create an environment¶
In order to manage environments, we need to create at least two so you can move or switch between them.
To create a new environment, use the conda create command, followed by any name you wish to call it:
conda create --name snowflakes biopython
When conda asks you
proceed ([y]/n)?
Type “y” for “yes.”
This will create a new environment named /envs/snowflakes
that contains the program Biopython. This environment will use the same version of Python that you are currently using, because you did not specify a version.
TIP: Many frequently used options after two dashes (--
) can be abbreviated with just a dash and the first letter. So --name
and -n
options are the same and --envs
and -e
are the same. See conda --help
or conda -h
for a list of abbreviations.
Change environments (activate/deactivate)¶
Linux, macOS: source activate snowflakes
Windows: activate snowflakes
Conda prepends the path name (snowflakes) onto your system command.
TIP: Environments are installed by default into the envs directory in your conda directory. You can specify a different path, see conda create --help
for details.
Deactivate the environment with the following:
Linux, macOS: source deactivate
Windows: deactivate
Conda removes the path name (snowflakes) from your system command.
Create a separate environment¶
So you can try switching or moving between environments, create and name a new environment. With this second environment, you can install a different version of Python, and a couple of packages:
conda create --name bunnies python=3 astroid babel
This will create a second new environment named /envs/bunnies with Python 3 and Astroid and Babel installed.
TIP: Install all the programs you will want in this environment at the same time. Installing one program at a time can lead to dependency conflicts.
TIP: You can add much more to the conda create command, type conda create --help
for details.
List all environments¶
Now you can use conda to see which environments you have installed so far. Use the conda environment info command to find out:
conda info --envs
You will see a list of environments like the following:
conda environments:
snowflakes /home/username/miniconda/envs/snowflakes
bunnies /home/username/miniconda/envs/bunnies
You can also use the conda environments list command as follows:
conda env list
The list of all environments will be the same with either command.
Verify current environment¶
Which environment are you using right now – snowflakes or bunnies? To find out, type the command:
conda info --envs
Conda displays the list of all environments, with the current environment highlighted with an ‘*’ character.
Clone an environment¶
Make an exact copy of an environment by creating a clone of it. Here we will clone snowflakes to create an exact copy named flowers:
conda create --name flowers --clone snowflakes
Check to see the exact copy was made:
conda info --envs
You should now see the three environments listed: flowers, bunnies, and snowflakes.
Remove an environment¶
If you didn’t really want an environment named flowers, just remove it as follows:
conda remove --name flowers --all
To verify that the flowers environment has now been removed, type the command:
conda info --envs
Flowers is no longer in your environment list, so we know it was deleted.
Saved environment variables¶
Conda environments can include saved environment variables on Linux, macOS, and Windows.
Suppose you want an environment ‘analytics’ to store a secret key needed to log in to a server and a path to a configuration file. We will write a script named env_vars
to do this.
Linux and macOS¶
Locate the directory for the conda environment, such as /home/jsmith/anaconda3/envs/analytics
. Enter that directory and create these subdirectories and files:
cd /home/jsmith/anaconda3/envs/analytics
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d
touch ./etc/conda/activate.d/env_vars.sh
touch ./etc/conda/deactivate.d/env_vars.sh
Edit the two files. ./etc/conda/activate.d/env_vars.sh
should have this:
#!/bin/sh
export MY_KEY='secret-key-value'
export MY_FILE=/path/to/my/file/
And ./etc/conda/deactivate.d/env_vars.sh
should have this:
#!/bin/sh
unset MY_KEY
unset MY_FILE
Now when you use source activate analytics
the environment variables MY_KEY and MY_FILE will be set to the values you wrote into the file, and when you use source deactivate
those variables will be erased.
Windows¶
Locate the directory for the conda environment, such as C:\Users\jsmith\Anaconda3\envs\analytics
. Enter that directory and create these subdirectories and files:
cd C:\Users\jsmith\Anaconda3\envs\analytics
mkdir .\etc\conda\activate.d
mkdir .\etc\conda\deactivate.d
type NUL > .\etc\conda\activate.d\env_vars.bat
type NUL > .\etc\conda\deactivate.d\env_vars.bat
Edit the two files. .\etc\conda\activate.d\env_vars.bat
should have this:
set MY_KEY='secret-key-value'
set MY_FILE=C:\path\to\my\file
And .\etc\conda\deactivate.d\env_vars.bat
should have this:
set MY_KEY=
set MY_FILE=
Now when you use activate analytics
the environment variables MY_KEY and MY_FILE will be set to the values you wrote into the file, and when you use deactivate
those variables will be erased.
More notes on environment variable scripts¶
These script files can be part of a conda package, in which case these environment variables become active when an environment containing that package is activated.
Scripts can be given any name, but multiple packages may create script files, so be sure that you choose descriptive names for your scripts that are not used by other packages. One popular option is to give the script a name of the form packagename-scriptname.sh (or on Windows packagename-scriptname.bat).
Next, we’ll take a look at Managing Python.