How to Install and Use Python Virtual Environments with Virtualenv

Learn how to install, configure and use Python virtual environments with virtualenv to isolate project dependencies, prevent package conflicts, and maintain consistent development environments across different machines.

How to Install and Use Python Virtual Environments with Virtualenv
Photo by Artem Bryzgalov / Unsplash

A Python virtual environment is essential for modern development, allowing you to isolate project dependencies and prevent version conflicts between projects. Learn how to properly install, configure, and manage virtualenv with Python 3 to ensure consistent code execution across different environments.

Getting Started with Virtual Environments in Python

Virtual environments are an essential tool in Python development. They create isolated spaces where you can install packages and dependencies specific to each project without affecting your global Python installation. This isolation helps prevent version conflicts when working on multiple projects that might require different versions of the same libraries.

Prerequisites for Setting Up Virtualenv

Before you can create and use virtual environments with virtualenv, make sure you have the following installed on your system:

Python Installation

Ensure you have Python 3 installed on your system.

Pip Installation

Pip is Python's package manager that you'll need to install virtualenv:

For Windows users:

  • Pip comes pre-installed with Python installations

For Linux users:

  • Install pip using the following command:
sudo apt-get install python3-pip

Installing and Setting Up Virtualenv

Follow these steps to install virtualenv and create your first virtual environment.

Installing the Virtualenv Package

Install virtualenv using pip with the appropriate command for your operating system:

# Windows
pip install virtualenv

# Linux
python -m pip install virtualenv

Creating a Virtual Environment

Navigate to your project directory and create a new virtual environment:

# Windows / Linux
python -m virtualenv venv

The venv parameter specifies the name of your configuration folder where all environment files will be stored. You can choose the name you want but venv is the standard name.

Activating Your Virtual Environment

Once created, you need to activate the virtual environment before using it:

# Windows
venv\Scripts\activate

# Linux
. venv/bin/activate

After activation, any packages you install will be isolated within this environment.

Managing Packages in Your Virtual Environment

When your virtual environment is active, you can manage packages specifically for your project.

Installing Packages

Install required packages using pip:

# Windows
pip install package_name

# Linux
python -m pip install package_name

Upgrading Pip

If you encounter installation errors, try upgrading pip within your virtual environment:

python -m pip install --upgrade pip

Verifying Package Installation

Check that your packages installed correctly:

# Windows
pip list

# Linux
python -m pip list
💡
It's normal that packages installed outside of your virtual environment won't appear in this list. This confirms the environment is properly isolated.

Working With Project Dependencies

Managing dependencies is crucial when working with virtual environments, especially when sharing projects.

Exporting Project Dependencies

To share your project with all its dependencies, create a requirements.txt file:

# Windows
pip freeze > requirements.txt

# Linux
python -m pip freeze > requirements.txt

This file contains all necessary packages and their versions for your project.

Project Recovery and Setup

There are different scenarios for setting up a project with virtual environments:

Using an Existing Virtual Environment

If the project already has a virtual environment:

  1. Download or clone the project
  2. Activate the existing virtual environment:
# Windows
venv\Scripts\activate

# Linux
. venv/bin/activate
  1. Begin working with the project

Setting Up a New Virtual Environment

If you need to create a new environment for an existing project:

  1. Download or clone the project
  2. Install virtualenv:
# Windows
pip install virtualenv

# Linux
python -m pip install virtualenv
  1. Create a new virtual environment:
# Windows / Linux
python -m virtualenv venv
  1. Activate the virtual environment:
# Windows
venv\Scripts\activate

# Linux
. venv/bin/activate
  1. Install required dependencies from requirements.txt:
# Windows
pip install -r requirements.txt

# Linux
python -m pip install -r requirements.txt
💡
If the project uses .gitignore to exclude the virtual environment folder, you'll need to create a new environment and install dependencies as shown above.

Deactivating Your Virtual Environment

When you've finished working on your project, deactivate the virtual environment:

deactivate

This returns you to your global Python environment.

Conclusion

Virtual environments are an indispensable tool for Python developers, providing clean separation between projects and their dependencies. By using virtualenv as demonstrated in this guide, you can create more maintainable and reproducible development environments. This approach helps prevent the notorious "it works on my machine" problem and makes collaboration much smoother.