Python Virtual Environments with Conda — Why the Buzz?

In this article, you’ll learn about an important concept in Python: virtual environments.

Why Virtual Environments?

Programs evolve over time. If you are a productive coder, you’ll ship your code early and often. Over time, you’ll fix the bugs as they appear and integrate feedback of users of your code.

And this is what all coders are doing. Suppose you are programming a crypto trading bot using a library to connect to the API of crypto exchanges (so that your bot can buy and sell currencies). An example of such a library is ccxt. Your code requires the exact API commands from ccxt (version 1.1). Say you install the ccxt library (version 1.1) on your computer.

A few years later, the ccxt library has changed (for example, there may be a new version 3.3) and you decide that it’s time to create a new bot. You install the new version 3.3 on your computer. But suddenly weird things begin to happen. Your old bot still runs as a background process (using ccxt version 1.1) but the new bot cannot start (using ccxt version 3.3).

Without virtual environments, you would have to decide for installing either version ccxt 1.1 or 3.3. Then, you would have to rewrite all old code using older versions.

But what if the library developer has removed a critical feature of your application?

Fortunately, Python provides you with virtual environments. A virtual environment allows you to define and install different dependencies for different projects. This is exactly what you need. You would install environment A for the first bot requiring version 1.1 and environment B for the second bot requiring version 3.3. That’s it!

Using virtual environments, you can separately and independently install different packages (even different versions of packages) for different Python projects. These installations are NOT visible globally. This has the advantage that you don’t clutter your global Python installation on your computer with myriads of possibly conflicting packages.

How to Create Virtual Environments?

There are many different tools to create virtual environments (virtualenv vs pipenv vs venv). But if you understood the conceptual idea introduced in the previous paragraphs, it’s easy to understand and use all of these tools.

Personally, I am using the Anaconda distribution which is a popular Python distribution which is a free and open source Python distribution specifically designed for scientific computing. In particular, Anaconda makes package management super easy.

As a side note: Catalina, one of the readers of my “Coffee Break Python” email course, pointed out that she uses the Spyder code editor from the Anaconda distribution. I agree that this editor provides some great features and makes writing code easier and more convenient than the default IDLE editor.

In the Anaconda distribution, you can use the “conda” tool to install new Python packages. The conda command combines the functionalities of the package manager pip with the functionalities of virtual environment tools such as venv. Roughly speaking, you can install new packages globally on your computer, as well as separately for each project in a virtual environment.

After installing the Anaconda distribution, you can simply run the following command in your shell to check whether conda is already installed:

conda -V

Then, you create your virtual environment by navigating to your project folder and executing the following command:

conda create -n yourproject python=3.7.3 anaconda

Just replace the dummy value “yourproject” with the name of your project and the Python version with your Python version. To check your Python version, simply run the command “python -V” in your shell.

Now, wait until your virtual environment is created. You should see a new folder “yourproject” at your file path.

At this point, you have created the virtual environment but it is not activated, yet. So simply execute the following command in your shell:

source activate yourproject

Now, you can install new packages specifically for this environment:

conda install -n yourproject scikit-learn

In this example, I’ve installed the scikit-learn package for machine learning. If you fail to specify the “-n yourproject” option, it will install the package globally and not in the virtual environment.

Now, you can simply run “python” in your shell and use the installed package “import sklearn”.

Let’s deactivate the virtual environment:

source deactivate

Puzzle: If you open a new python shell now, can you still import the sklearn library?*

If you haven’t installed the sklearn library before, the answer is no! The scikit-learn library has only been installed in the virtual environment. But you have deactivated the virtual environment using the previous command.

Leave a Comment