4 min read

Why do I need a virtual environment?

Could an update to a package stop a project from working? How do you manage your virtual environments? How many is too many? Doesn't that take up a lot of space though?
Software dependencies. Credit: https://imgs.xkcd.com/comics/dependency.png

What is a virtual environment?

The official Python documentation states:

"A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system"

What does this mean?

It means that each virtual environment it's self contained. It has a specific version of the Python interpreter, its own pip and a set of libraries that were installed only on that environment.

Visual representation of virtual environments

We can thing of virtual environments roughly as below.

On my machine I have the system Python which has its own pip and packages. Whatever I install there, it's available globally. Meaning, when a virtual environment is not active the Python used will be the system Python and therefore, the installed libraries will be also available. When a Python virtual env is active it means that the Python used will be the version specified in the virtual env and also the libraries available for that version of the Python is only the libraries installed in that specific virtual environment.

diagram of virtual environments and the system Python

Let's assume that the CacheControl library is used by MacOS for caching requests in Safari. The version installed globally supports a functionality that is not now removed in version 0.13.1 . If I update to the latest version most likely I will break Safari.

If you like the content here, please consider subscribing. No spam ever!

Could an update to a package stop a project from working?

Definitely! This happens more often than you think if you are working in a big project were a lot of developers are collaborating all at the same time. Even if you are the only person working on a project this can still happen very easily. Software is always changing and evolving. Therefore, updating a python library can break your project.

How do you manage your virtual environments?

There are a lot of tools to manage your virtual environments. But, there's a big BUT. I would recommend to start with the official way that Python documentation says instead of using one of these tools especially in the beginning when you are trying to learn Python and its ecosystem.

Creating virtual environments

The official way of creating a virtual environment is:

python3 -m venv /path/to/new/virtual/environment

Usually, what I like to do is:

  1. Navigate to the project's folder
  2. Run python -m venv .project_name. This will create a folder containing the virtual env inside the project's folder

Activating a virtual env

You can do:

source project_name/bin/activate

Activating virtual env python

Because I am using zsh with the oh-my-zsh plugin I can see on the right side that my virtual environment is activated.

Deactivating a virtual environment

You can do:

deactivate

Deactivating virtual env python

Again, because I am using zsh with the oh-my-zsh plugin I can see that the previously activated env is no longer active.

If you like the content here, please consider subscribing.

How many is too many?

There's not such a thing as too many. A good rule of thumb is to use one virtual environment per project. Some colleagues are using one environment for multiple projects but from my own experience I found this to be problematic.

So to summarise, just use one virtual environment per project (repository).

Doesn't that take up a lot of space though?

The short answer is no.

Each virtual environment has an overhead of about 25Mb of storage plus each library that you'll install to your virtual environment.

Does a python virtual environment avoid redundant installs?

No.

That's the main point. That each virtual environment is isolated therefore if you have the same library in more than one virtual environments it will be installed in your machine for each environment.

Are you supposed to delete the virtual environment when you’re done?

I don't think there's such a thing as "you're done" in the software engineering world. If you delete a project and the virtual environment was created in the same folder as your project then it will be deleted as well. Because it's just a folder in your project's directory. If you created the virtual environment in a different directory, it means that you have to delete it manually.

If you like the content here, please consider subscribing.

Is it necessary when you are learning Python to create a virtual environment?

No, you can use something like Replit and not even have to worry about having a Python installation in your machine. However, the first time that you want to install a library consider creating a virtual environment.

Especially, if you are using an OS that uses Python as part of the OS. For example, in MacOS if you install something it might messed up the system Python which will have catastrophic consequences for your operating system. You might end up destroying your system. Not to mention that you are exposing your system to security vulnerabilities as well.

Conclusion

Python virtual environments are the solution to manage dependencies between projects and your system. This makes the development process easier and reproducible which is a huge benefit.

When creating a project, as soon as you want to install an external dependency (library) using pip, it's a good idea to create a virtual env.

The end

Congratulations! You made it to the end.

If you like the content here, please consider subscribing. No spam ever!

If you have any comments or feedback feel free to reach me @costapiy

Some useful links to consider if you want to learn more about virtual environments:

  1. Python official documentation
  2. Freecodecamp
  3. RealPython
  4. 8 Must-Know venv Commands For Data Scientists and Engineers
  5. How To Work With VS Code And Virtual Environments In Python