When you start working on multiple projects in your local development environment, you’ll often run into dependency issues. Creating virtual environments for each of your projects can help you manage dependencies and project requirements better. To leverage the advantage of virtual environments in Python, let’s learn to create and activate virtual environments. 👩🏽‍💻

What Are Virtual Environments?

But why should you use virtual environments? Well, virtual environments allow you to install and use different versions of the same libraries for various projects. Using virtual environments also ensures that there are no breaking changes when two or more projects use different versions. Let’s understand this in greater detail.

Installing Packages in Python

The Python standard library ships with several useful modules for unit testing, interacting with the operating system, working with dates and times, and more. However, when working on Python projects, you’ll often need to install other packages – developed by the Python community. This is especially true for applications such as web scraping for data collection, machine learning, and web applications. To install and manage these packages, you can use conda or pip. Each project requires a specific set of packages that you need to install. However, when you install all the packages in your development environment on your local machine, all the projects share the globally installed packages.

So why is this a problem?

Well, you may have N packages in your development environment. However, the project that you are working on currently may require only 3 of them. When all your projects share common installations, it is very difficult to identify which of the projects required which of the installed packages – the dependencies associated with each project. There’s another limitation to this approach. Suppose you have a Django 2.2 project in your project library. You decide to start working on a project that uses Django 4. So you install the most recent version of Django in the same development environment.

What happens to the existing installation?

It is removed and replaced by the version of Django you installed. With newer stable releases, certain features might have been deprecated. And your earlier Django projects may not work as expected. Summing up our discussion thus far: dependency management becomes difficult when the packages are all installed in a common environment because the projects require their own set of libraries to run.

How Virtual Environments Work

So far, we have seen the challenges associated with installing packages in a global development environment (system-wide installations). This motivates us to understand how virtual environments address this limitation. When you create and activate a virtual environment for your Python projects, you can install only those packages that are required for the current project. Revisiting the Django projects example, with virtual environments, you can have both Django 2.2 and Django 4 projects run – without any conflict. This is because the Django installations are no longer system-wide installations but are confined to the virtual environments of the respective projects. In essence: virtual environments are isolated environments that contain both the code and the dependencies for a project.

Advantages of Virtual Environments

Now that you’ve learned how virtual environments work in Python, let’s enumerate the advantages of using them:

Virtual environments provide an isolated development environment for individual projects allowing us to install only the packages needed by required for the specific project. As the virtual environments of projects are both independent and isolated,  different projects can use different versions of the same library—depending on the requirements. With virtual environments, you don’t have to worry about system permissions to install libraries and set up the development environment. Once you install packages in a virtual environment, you can capture the project’s dependencies in a requirements.txt file. This allows other developers to replicate the project’s development and environment, and install the required packages using a single command.

Tools to Create Virtual Environments

So far, you’ve learned how virtual environments work and the advantages of using them. Let’s explore some popular tools you can use to create and manage virtual environments in Python.

#1. Virtualenv

Virtualenv is one of the widely used tools for creating and managing virtual environments for Python projects. A subset of the functionality of virtualenv is available in the venv package. However, the virtualenv package is faster and extensible as compared to venv.

#2. Pipenv

With pipnev, you have both the virtual environment functionality of virtualenv and package management capabilities of pip. It uses manages pipfiles to manage project dependencies inside of a virtual environment using. You can try out pipenv right from the browser on this Pipenv playground.

#3. Conda

If you use the Anaconda distribution of Python for development, then you can use conda for package management and for creating virtual environments. To learn more, check out this comprehensive guide on managing environments with conda.

#4. Poetry

Poetry is a package management tool that lets you manage dependencies across all Python projects. To start using Poetry, you need to have Python 3.7 or a later version installed.

#5. Venv

As mentioned, venv offers a subset of the functionality of virtualenv but it comes with the advantage that it is built into the Python standard library, starting from Python 3.3. It is readily available with the Python installation—and does not require installation of external packages. We’ll use it in this tutorial to create and work with virtual environments. ✅

How to Create a Python Virtual Environment in Ubuntu

This section outlines the steps to create and activate virtual environments in an Ubuntu Linux machine. The same steps can be used on other Linux distros as well. For easier management, let’s create a project directory and cd into it; We’ll create venv inside this directory. The general syntax to create a virtual environment for your Python project is python3 -m venv . Upon running this command a virtual environment called my_env will be created in the current working directory:

How to Activate and Install Packages Inside a Virtual Environment

After you’ve created the virtual environment, you can activate it and install required packages in it. To activate virtual environment you can run the following command: Once you’ve activated a virtual environment, you can run the pip list command to get the list of installed packages: So far we’ve not installed any package, so you should be able to see setuptools and pip—installed by default—in each of the created virtual environments. The installation of pip inside the virtual environment allows you to install packages needed for the specific project; this is why you have an independent development environment for each project. Now that you have activated the virtual environment, you can install project-specific packages in it using pip. As an example, let us install Python requests, one of the most downloaded Python packages, which provides several useful features to send HTTP requests for working with web APIs. When you install the requests library, you’ll see that the requests library gets installed along with all the packages that are required by it. You can use the pip freeze command and redirect the output to a requirements.txt file, as shown: If you examine the contents of the current project directory, you’ll see that the requirements.txt file has been created. You can deactivate the virtual environment after you’ve worked on the project by running the following command:

How to Create a Python Virtual Environment in Windows

In general, a Linux environment is preferred for development. If you’re on a Windows machine, you can consider using the Windows Subsystem for Linux (WSL) to set up an Ubuntu terminal environment for local development. If you’re on a Windows machine, you can use either the Windows PowerShell or the Command Prompt and create virtual environments using the following command:

How to Activate a Virtual Environment

Activating virtual environments on a Windows machine is different depending on whether you’re working on the command prompt or the Windows PowerShell. If you are on the command prompt, run the following command to activate the virtual environment: Alternatively, if you’re using Windows PowerShell, running this command will activate the virtual environment: You can install all the required packages inside the virtual environment. To deactivate virtual environments, you can run the deactivate command—both on the command prompt and Windows PowerShell.

Conclusion

In this article, we discussed the limitations of system-wide installations and how they make dependency management across Python projects difficult. Virtual environments in Python provide a way to manage dependencies better while providing an isolated development environment for individual projects. Among the widely used tools for creating and managing virtual environments in Python, you’ve learned how to use venv – that is built into the Python standard library to create and activate virtual environments. Inside a project’s dedicated virtual environment, versions of libraries specific to a project can be installed. These requirements can then be captured in a requirements.txt file, which allows other developers to replicate the project environment easily. When you start your next Python project, be sure to use virtual environments for better dependency management. Happy coding!🎉

Getting Started with Virtual Environments in Python - 25Getting Started with Virtual Environments in Python - 44Getting Started with Virtual Environments in Python - 72Getting Started with Virtual Environments in Python - 77Getting Started with Virtual Environments in Python - 2Getting Started with Virtual Environments in Python - 70Getting Started with Virtual Environments in Python - 27Getting Started with Virtual Environments in Python - 30Getting Started with Virtual Environments in Python - 82Getting Started with Virtual Environments in Python - 74Getting Started with Virtual Environments in Python - 15Getting Started with Virtual Environments in Python - 54Getting Started with Virtual Environments in Python - 81Getting Started with Virtual Environments in Python - 24Getting Started with Virtual Environments in Python - 24Getting Started with Virtual Environments in Python - 30Getting Started with Virtual Environments in Python - 57Getting Started with Virtual Environments in Python - 35Getting Started with Virtual Environments in Python - 67Getting Started with Virtual Environments in Python - 9Getting Started with Virtual Environments in Python - 83Getting Started with Virtual Environments in Python - 20Getting Started with Virtual Environments in Python - 6Getting Started with Virtual Environments in Python - 57Getting Started with Virtual Environments in Python - 79Getting Started with Virtual Environments in Python - 28Getting Started with Virtual Environments in Python - 97Getting Started with Virtual Environments in Python - 13Getting Started with Virtual Environments in Python - 44Getting Started with Virtual Environments in Python - 22Getting Started with Virtual Environments in Python - 9