Python - Python Virtual Environments (venv & virtualenv)
Python virtual environments are a way to create isolated spaces for Python projects so that each project can have its own dependencies, libraries, and versions without interfering with others. This solves one of the most common problems in Python development—dependency conflicts.
When you install packages globally using pip, they are shared across all Python projects on your system. This becomes problematic when different projects require different versions of the same library. A virtual environment ensures that each project operates in its own self-contained setup, independent of the global Python installation.
Why Virtual Environments Are Important
In real-world development, you may work on multiple projects simultaneously. For example, one project might require Django version 3.x, while another requires Django 4.x. Installing both versions globally would create conflicts. Virtual environments prevent this by isolating dependencies.
They also improve reproducibility. When you share your project with others, you can provide a list of dependencies, and they can recreate the same environment on their system. This ensures consistency across development, testing, and production.
Built-in Tool: venv
Python provides a built-in module called venv (available from Python 3.3 onwards) to create virtual environments.
To create a virtual environment:
python -m venv myenv
This command creates a folder named "myenv" containing a private Python interpreter and a copy of pip.
To activate the environment:
On Windows:
myenv\Scripts\activate
On macOS/Linux:
source myenv/bin/activate
Once activated, any packages you install using pip will be installed only inside this environment.
To deactivate the environment:
deactivate
Using virtualenv (Alternative Tool)
Before venv became standard, developers used a third-party tool called virtualenv. It is still widely used because it offers additional flexibility and supports older Python versions.
To install virtualenv:
pip install virtualenv
To create an environment:
virtualenv myenv
The activation and usage process is similar to venv.
Managing Dependencies
Inside a virtual environment, you can install packages using pip:
pip install requests
To save dependencies:
pip freeze > requirements.txt
This creates a file listing all installed packages and their versions.
To recreate the environment later:
pip install -r requirements.txt
This is essential for team collaboration and deployment.
Directory Structure of a Virtual Environment
A typical virtual environment contains:
-
A copy of the Python executable
-
A Scripts (Windows) or bin (Linux/macOS) folder for executables
-
A site-packages directory where installed libraries are stored
This structure ensures complete isolation from the global Python environment.
Best Practices
Always create a virtual environment for every new project, even small ones. Name the environment clearly, often as "venv" or ".venv". Do not commit the environment folder to version control; instead, include the requirements.txt file. Activate the environment before installing or running project dependencies to avoid accidental global installations.
Summary
Python virtual environments are a fundamental tool for managing dependencies, avoiding conflicts, and ensuring consistent development setups. Whether using venv or virtualenv, they help maintain clean, isolated, and reproducible project environments, which is essential in both individual and team-based development.