Python virtual environments for scientists with conda part3

In our first post we learned about Python virtual environments and how to use conda to create and manage them. In our second post, we learned how to install packages that were not directly accessible from the command conda install. Specifically, we learned about conda channels hosted at https://anaconda.org as well as pip, both of which can be used to install Python packages.

In the post, we will review some useful conda commands to manage our various Python virtual environments.

Review: making new Python virtual environments

In order to learn about useful conda commands, let’s assume that we have already created the Python virtual environment called sci_sound from first post. To review how to make a new Python virtual environment, we will also make two new environments.

The first new environment will be called cool_project. It will be a Python 3.7 environment, and include three packages: pandas, matplotlib and ipython.

(base) /home/martin$ conda create --name cool_project python=3.7 pandas matplotlib ipython

The second new environment will be called bokeh_dev. Again it will be a Python 3.7 environment, but this time we will include bokeh, a data visualization package.

(base) /home/martin$ conda create --name bokeh_dev python=3.7 bokeh

What environments are currently on our system?

To determine what conda Python virtual environments are currently on our system, we can run the following command:

(base) /home/martin$ conda info --envs
# conda environments:
#
base * /home/martin/anaconda3
bokeh_dev /home/martin/anaconda3/envs/bokeh_dev
cool_project /home/martin/anaconda3/envs/cool_project
sci_sound /home/martin/anaconda3/envs/sci_sound

Nice! We see that we have our base installation of Anaconda, which is our currently active Python environment.
How do we know it is currently active?
First, our terminal prompt shows use our current active environment in parentheses (base).
Also, the command conda info --envs marks our active Python environment with an asterisk.

We also have sci_sound, the environment we created in our first post in this series, as well as the two new environments we just created.

What packages are installed in my environments?

There may come a time when we can’t remember exactly what packages we have installed in a given Python virtual environment.
We can use the command conda list to list all the packages installed in a given Python environment.

If we can conda list with no other arguments, conda will list all the packages for the currently active environment.
However, we can provide the --name flag to specify the name of one of our environments.
Let’s give it a try!

(base) /home/martin$ conda list --name cool_project
# packages in environment at /home/martin/anaconda3/envs/cool_project:
#
# Name Version Build Channel
[...]
ipython 7.5.0 py37h24bf2e0_0 conda-forge
[...]
matplotlib 3.1.0 py37_1 conda-forge
[...]
numpy 1.16.4 py37h95a1406_0 conda-forge
[...]
pandas 0.24.2 py37hb3f55d8_0 conda-forge
[...]
python 3.7.3 h5b0a415_0 conda-forge

To save space, the names of dozens of helper packages have been removed from the list.
The key thing to notice is that the list includes the version of Python we asked for, as well as ipython, matplotlib and pandas.

But wait, do you notice something strange?
numpy appears in this list despite not telling conda that we wanted to include this (very useful) package when we created our virtual environment?

numpy was installed because pandas relies heavily on many aspects of numpy.
So conda installed a compatible version of numpy for us.

How do we remove unwanted environments?

When we are first learning about Python virtual environments, we often have many practice environments that we need to delete.
This is as simple as conda remove --name env_name --all, where env_name is the name of the environment we want to delete.

As an example, let’s remove the cool_project environment we created earlier.

(base) /home/martin$ conda remove --name cool_project --all

Remove all packages in environment /home/martin/anaconda3/envs/cool_project:

## Package Plan ##

environment location: /home/martin/anaconda3/envs/cool_project

The following packages will be REMOVED:

[...]

Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done

We can confirm this work by listing our available environments.

(base) /home/martin$ conda info --envs
# conda environments:
#
base * /home/martin/anaconda3
bokeh_dev /home/martin/anaconda3/envs/bokeh_dev
sci_sound /home/martin/anaconda3/envs/sci_sound

Summary

This post introduced three new conda commands:
conda info --envs to list the environments that currently exist on our system.
conda list --name env_name to list the packages installed in a given environment.
conda remove env_name --all to remove the environment from our system.

In our next post we will learn how conda can help us with our efforts to be more reproducible scientists.
Specifically, we will learn how to create a file that others can use to replicate our Python virtual environment on their machine.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s