Skip to main content

Python Virtual Environments

Introduction

When working on multiple Python projects, you might encounter situations where different projects require different versions of the same library. For instance, one project might need PyTorch 1.8 while another requires PyTorch 2.0. This is where virtual environments come to the rescue.

A virtual environment is an isolated Python environment that allows you to install packages without affecting your system-wide Python installation or other projects. Think of it as a separate "container" for each of your Python projects, with its own set of packages and dependencies.

In this tutorial, we'll learn:

  • Why virtual environments are essential
  • Different tools to create virtual environments
  • How to create, activate, and manage virtual environments
  • Best practices for dependency management

Why Do We Need Virtual Environments?

Imagine these scenarios:

  1. Dependency Conflicts: Project A needs version 1.0 of a library, while Project B needs version 2.0.
  2. System Pollution: Installing packages globally can clutter your system Python installation.
  3. Reproducibility: Ensuring your code works the same way on different machines.

Virtual environments solve these problems by creating isolated spaces for your projects.

Tools for Creating Virtual Environments

There are several tools available for creating virtual environments in Python:

  1. venv: Built into Python 3.3+ (what we'll focus on)
  2. virtualenv: A third-party tool that works with Python 2 and 3
  3. conda: An environment manager that comes with Anaconda/Miniconda
  4. pipenv: Combines pip and virtualenv functionality

Let's explore the most common ones.

Using venv (Python's Built-in Tool)

Creating a Virtual Environment

To create a virtual environment, navigate to your project directory and run:

bash
python -m venv myenv

This creates a new directory called myenv that contains:

  • A copy of the Python interpreter
  • The pip package manager
  • A few other files for environment configuration

Activating the Virtual Environment

On Windows:

bash
myenv\Scripts\activate

On macOS/Linux:

bash
source myenv/bin/activate

When activated, you'll see the environment name in your terminal prompt:

(myenv) C:\Users\username\project>

Installing Packages

Once your environment is activated, you can install packages using pip:

bash
pip install numpy
pip install torch torchvision

These packages will only be available in this virtual environment.

Deactivating the Virtual Environment

When you're done working on your project, you can deactivate the virtual environment:

bash
deactivate

Your terminal prompt will return to normal.

Managing Dependencies

Saving Dependencies

To save a list of your project's dependencies:

bash
pip freeze > requirements.txt

This creates a text file with all installed packages and their versions:

numpy==1.21.0
torch==1.9.0
torchvision==0.10.0

Reinstalling Dependencies

On another machine or after recreating your environment, you can install all dependencies:

bash
pip install -r requirements.txt

Using conda for Virtual Environments

If you're using Anaconda or Miniconda, you can use conda to manage virtual environments:

Creating a Conda Environment

bash
conda create --name myenv python=3.9

This creates a new environment with Python 3.9.

Activating a Conda Environment

bash
conda activate myenv

Installing Packages

bash
conda install numpy
conda install pytorch torchvision -c pytorch

Deactivating a Conda Environment

bash
conda deactivate

Exporting and Importing Environment Configurations

Export:

bash
conda env export > environment.yml

Import:

bash
conda env create -f environment.yml

Real-World Example: Setting Up a PyTorch Project

Let's walk through setting up a virtual environment for a PyTorch project:

  1. Create the environment:

    bash
    python -m venv pytorch_project
  2. Activate the environment:

    bash
    # On Windows
    pytorch_project\Scripts\activate

    # On macOS/Linux
    source pytorch_project/bin/activate
  3. Install PyTorch and related packages:

    bash
    pip install torch torchvision torchaudio
    pip install matplotlib numpy pandas jupyter
  4. Save your environment:

    bash
    pip freeze > requirements.txt
  5. Create a simple script to test your environment:

    python
    # test_torch.py
    import torch
    import torchvision

    print(f"PyTorch version: {torch.__version__}")
    print(f"CUDA available: {torch.cuda.is_available()}")

    # Create a simple tensor
    x = torch.rand(5, 3)
    print("Random tensor:")
    print(x)
  6. Run your script:

    bash
    python test_torch.py

    Output:

    PyTorch version: 1.9.0
    CUDA available: True
    Random tensor:
    tensor([[0.3126, 0.8236, 0.5341],
    [0.0402, 0.7333, 0.1469],
    [0.6339, 0.9061, 0.1292],
    [0.9457, 0.9593, 0.4185],
    [0.2307, 0.1672, 0.7235]])

Best Practices

  1. Create a new virtual environment for each project
  2. Always activate your environment before working on your project
  3. Keep your requirements.txt file updated
  4. Include instructions on how to set up the environment in your project's README
  5. Consider adding environment directories to your .gitignore file

Troubleshooting Common Issues

"The term 'activate' is not recognized"

Make sure you're using the correct activation command for your operating system, and that you're in the correct directory.

Packages not found after installation

Verify that your virtual environment is activated (check for the environment name in your terminal prompt).

Permission errors during installation

You might need administrator privileges. Try:

bash
pip install --user package_name

Summary

Virtual environments are an essential tool for Python development, especially when working with PyTorch and other data science libraries. They help you:

  • Keep dependencies isolated between projects
  • Manage different versions of packages
  • Create reproducible environments for sharing your code
  • Keep your system Python installation clean

By incorporating virtual environments into your workflow, you'll avoid common dependency issues and make your projects more portable and maintainable.

Exercises

  1. Create a virtual environment and install PyTorch. Write a script to check that PyTorch is correctly installed.
  2. Create two different virtual environments with different versions of the same library. Observe how they work independently.
  3. Export the dependencies from a virtual environment and recreate it on another location.
  4. Try using both venv and conda to create virtual environments. What differences do you notice?

Additional Resources

Now you're ready to properly manage your Python environments for your PyTorch projects!



If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)