Exploring virtual environments in Python: Part 2

Ashwin Nair
3 min readSep 29, 2020

This is part two of virtual environments in Python. Part one can be found here.

In the last article, we learned about using virtual environments with the standard library module venv. and we explored some of the commands you could use to customize your virtual environment. This time we will be using virtualenv[ 1], a pip module that creates and manages virtual environments. It has more features than the venv module, so let's get started.

First, create a new directory for testing purposes, like so:

The testing directory.

This is a very simple directory, with a singular python file inside. This file’s contents are shown below:

Contents of a Python file.

Once you’ve entered the directory, type the following code in your terminal:

Installing virtualenv.

Reminder: We use the pip3 installer to specify which Python installation we'd like to download for.

Now that virtualenv has been installed, we can use the basic env creating a command to create the environment:

Creating a virtual env.

There’s no console output from this command, so you may have to close your directory and open it again to see if it has worked. Also, note that you can call the virtual environment something else than env; this is just a very common name for virtual environments.

If your directory looks like this, you’ve succeeded! If you used a different name than env for your environment, your newly created directory name may vary.

Now that your virtual environment is created, let’s activate it with source env/bin/activate. Remember if your virtual environment's name is not env, substitute env with your environment's name.

Activating the virtual environment

As you can see, there’s no console output from this command, but if you pay close attention, you can see that something has changed! There is now (env) before the prompt, signifying that we're now in the virtual environment. Try to install a package via pip to test it out:

Installing a pip package.

Now we could do all of this with venv but there are perks to doing this with virtualenv instead.

  • venv is slower
  • venv cannot be upgraded via pip, since it is a module of the standard library that ships with Python
  • venv only works with Python 3.3 or higher; virtualenv will work from Python 2.7 onwards

This is very important; since venv is a batteries-included module, this means that it cannot be updated, whereas virtualenv can be updated. It's also important to note that working with distributions of Python before 3.3 is an important feature.

To update virtualenv, simply type:

Updating virtualenv.

Success! Now you can use the pip commands covered in article one. Another big advantage to using virtualenv is that there are many add-ons made for it. One such add-on would be nox[ 2], which is a command-line tool for testing within a virtualenv environment.

In this article, we have reviewed the venv module and also learned about an alternative: The virtualenv module. We also learned why using virtualenv can have its advantages. Thanks for reading, and stay tuned for more articles.

References

  1. virtualEnv Docs — pypa.io
  2. nox Docs — PyPi

--

--

Ashwin Nair

Pursuing my bachelors in Computer Science, while learning Natural Language Processing with SpaCy and Python.