Sharable Python virtual environments with a single Terminal command
Setting up virtual environments from scratch every time is a tedious and time-consuming process. There’s a way around it. You can use Anaconda to export a Python virtual environment to a YAML file you can then reuse in different projects. You can also share the YAML file with your team, so everyone’s on the same track.
Today you’ll learn how. It’s easier than you think, so let’s get right to it.
Don’t feel like reading? Watch my video instead:
Create and Setup an Anaconda Environment
Anaconda makes it extra easy for data scientists to set up virtual environments. I’m using a scaled-down version called Miniforge, which doesn’t ship with a ton of libraries preinstalled. It doesn’t matter which flavor you’re using.
We’ll start by creating a new environment named
test_env based on the latest version of Python 3.9. Open up a Terminal window and type in the following:
conda create --name test_env python=3.9 -y
The initial setup won’t take long. You’ll see the following message displayed after a couple of seconds:
Which means the environment is successfully installed. You can activate it with the following command:
conda activate test_env
You now have everything needed to install a couple of libraries. Let’s stick with the basic ones — Numpy, Pandas, Matplotlib, Scipy, Scikit-Learn, and Jupyter:
conda install -c conda-forge -y numpy pandas matplotlib scipy scikit-learn jupyter jupyterlab
This could take a while, depending on your Internet speed. Anaconda has to download all listed libraries and their dependencies, so let it do its thing. Once done, you should see something similar displayed:
That’s how you’d normally approach setting up virtual environments from scratch. Let’s see how you can export it next.
How to Export and Load Anaconda Virtual Environments
Anaconda allows you to export a virtual environment into a YAML file. It’s simple to understand data serialization language often used to create configuration files. YAML is similar to JSON, but without brackets.
Use the following command to export the environment, just remember to modify the export path:
conda env export > \Users\Dario\Desktop\test_env.yaml
You can open the YAML file with any text editor, for example, Visual Studio Code. Here’s what it contains:
The file contains complete instructions on how to set up an identical environment with Anaconda. The only problem is the environment name displayed on the first line. You’ll have to change it as you already have an environment called
test_env. Let's keep things simple and rename it to
You can now use the following command to create a virtual environment from the YAML file:
conda env create -f \Users\Dario\Desktop\test_env.yaml
The command shouldn’t take long to execute, as you already have all the libraries downloaded. Once finished, you’ll see a similar message telling you how to activate the environment:
Use the displayed command to activate the environment:
conda activate test_env_2
Finally, let’s verify we have the Python libraries installed. Start a Python shell and import everything previously installed — Numpy, Pandas, Scipy, Matplotlib, and Scikit-Learn:
It seems like everything works as expected, you have a new environment packed with the dependencies from the YAML file. Easy, right?
And there you have it — reproducible Anaconda environment without breaking a sweat. You can use them to set up base environments you can then tweak according to the project needs. For example, pretty much every data science project requires the libraries we installed today, so why not save yourself a couple of minutes.
Another use case is the ability to share the YAML file with your team, so everyone is on the same track.
Either way, it’s a good-to-know feature of Anaconda.