Python Friday #20: Creating Virtual Environments

As explained in the last post, pip allows you to only keep one version of a package. If you cannot keep every dependency up to date at all the times, you run into a problem. Fortunately for us, Python offers a solution for that.

This post is part of my journey to learn Python. You can find the other parts of this series here.

 

What is a virtual environment?

A virtual environment is an isolated working copy of your Python environment. What you install and change there only affects this one environment and not the other ones (or your system). This allows you to use a different version of a package in your project A than you use in project B. You can find a more detailed explanation on how this works in this article from Real Python.

 

Creating a virtual environment on Windows

First, we need to install the virtualenv package on our system:

Our virtual environments are a collection of files and binaries, that we need to put somewhere. Since they grow with the packages we install, we should put them somewhere outside of our project. I created a folder Python_VirtualEnv on my D: drive:

We can create a virtual environment by calling virtualenv with the name we want to give. The name is also the location where all the files for your virtual environment are saved:

We can start this environment by running the activate script in our environment folder:

As you can see, the name of your virtual environment is now part of the command prompt. This is especially helpful when you work with multiple projects in multiple windows. You can check what packages are installed by running pip list:

You can install packages in this environment as you like. You can work in any folder on your system, you are not limited to the one in which you created your virtual environment.

When you are done or want to switch to another project or, you first deactivate this virtual environment:

 

Creating a virtual environment on Linux

On Linux we start with installing virtualenv as well. If there are problems, you may for this one package use sudo to install it system-wide:

We create a folder for our environment in our home directory:

To create a new environment, we again need to run virtualenv with a name:

To start our environment, we need to run the activate script located in the bin folder. Make sure that you add a . in front of the command to change your current session in your terminal:

To check if we are inside our virtual environment, we run again pip list:

We can leave our virtual environment with the command deactivate:

 

Customizing your virtual environment

The commands I used above will use the default version of Python on your system to create that virtual environment. If you have multiple versions of Python on your system you may want to specify which one to use. The user guide of virtualenv lists all the possibilities you have and offers an overview on advanced topics like Seeders to install some seed packages in all your environments.

 

Alternatives

There are different projects trying to simplify the use of virtual environments. Two often named tools are Poetry and pipenv. Poetry has a nice documentation and pipenv is the recommended tool in the Python Packaging User Guide, but the last release at the time of writing this post is 2018.11.26 and it looks as if there are various problems that needs resolving.

 

Conclusion

Virtual environments are in my opinion a great help to keep your dependencies in order. I am still new to Python and for me the “old” approach with virtualenv works well enough. If you run into problems or need more comfort, you should check out alternatives like Poetry.

7 thoughts on “Python Friday #20: Creating Virtual Environments”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.