Understanding Python Virtual Environments

Devashish Patil
CodeByte
Published in
3 min readAug 19, 2022
Photo by Lorenzo Herrera on Unsplash

The need for virtual environments

There comes a time in your software development journey when you are working on multiple Python projects, be it for work or some personal project.

As we start working on Python projects and as the projects grow, the project dependencies start increasing which we generally install via pip or conda. These dependencies are project level and there might be cases where one project needs a specific version of a library, while the other project needs another version of the same library.

Since Python libraries are installed on your machine globally by default and that too is only one version of each library. This will create conflicts and one of the two projects will not work.

This is a big problem. Let’s see what options we have.

What options do we have?

The approach here is to use multiple isolated environments/machines for each project. But you can’t have 5 computers or virtual machines for 5 projects that you are working on. That is not a practical solution either.

Here comes the idea of Python Virtual Environments. These virtual environments are isolated python workspaces. You can create multiple environments for each of your projects. All the project dependencies that you install in one environment will be independent of the ones installed globally or in other python virtual environments.

This diagram helps you visualize how multiple environments can help you develop more than one project in the same machine

Designed by Devashish Patil

How to use Python Virtual environments

You can use venv or virtualenv modules for creating python virtual environments. For this tutorial’s purpose, we’ll be using this inbuilt python module called venv. You need to have python installed in your system for this and that’s it.

This is how you can create the virtual environment:

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

Once you have the environment created, you need to set the context in your terminal/editor for this environment by activating it. This is how you can do it in Linux/Unix-based machines:

source /path/to/new/virtual/env_name/bin/activate

For activating the virtual environment in a Windows machine, you will need to run this:

CMD:

\path\to\new\virtual\env_name\Scripts\activate.bat

Powershell:

/path/to/new/virtual/env_name/Scripts/Activate.ps1

Once this is successful, you’ll be able to see a (env_name) in front of your terminal command line like this:

Now, any installation that you do via pip will only be installed inside this virtual environment which you will be able to see inside the environment directory. You can start using these environments for your projects now.

Congratulations, you have successfully learned how to use Python Virtual Environments. Let me know your thought in the comments or join this discord server for more discussion.

--

--