Table of Contents

    Virtual Environment (venv)

    CHAPTER 28.4 · PYTHON PACKAGE MANAGEMENT

    Virtual Environment in Python

    Learn how to create, activate, use, deactivate, and recreate an isolated Python environment using the built-in venv module.

    Different Python projects may require different packages or different versions of the same package. Installing every dependency into one global Python installation can create package conflicts.

    A virtual environment provides a separate Python environment for an individual project. Packages installed in one virtual environment do not normally affect packages installed in another environment.

    Learning objective: In this tutorial, you will learn why virtual environments are important, how to create and activate them, and how to install project-specific packages safely.

    Prerequisites

    What You Need

    • Python 3 should be installed on your computer.
    • Python should be accessible from Command Prompt, PowerShell, Terminal, or another command-line shell.
    • You should understand basic terminal commands such as cd.
    • Basic knowledge of pip and Python package installation is helpful.
    • A separate project folder should be available.

    What is a Virtual Environment?

    A virtual environment is a project-specific Python environment. It contains a Python executable, activation scripts, configuration information, and a separate location for installed packages.

    Python provides the built-in venv module for creating lightweight virtual environments.

    Think of it as a separate toolbox

    Every project receives its own toolbox containing the exact packages and versions it needs. Updating one toolbox does not change the contents of another.

    Why Use a Virtual Environment?

    Without a Virtual Environment

    • Unrelated projects may share the same packages.
    • Upgrading a package may unexpectedly affect another project.
    • Project-specific dependencies become difficult to identify.
    • Reproducing the project environment becomes harder.

    With a Virtual Environment

    • Each project maintains its own dependencies.
    • Different projects can use different package versions.
    • Package changes remain isolated to the selected project.
    • The environment can be recreated from a dependency file.

    Example of a Version Conflict

    Suppose two Python projects require different versions of the same package:

    Project Package Required Version
    Project A Django Version 4.x
    Project B Django Version 5.x

    Separate virtual environments allow both projects to use their required versions without interfering with each other.

    Check the Python Installation

    Open your command-line application and check the installed Python version.

    python --version

    On some systems, use one of the following commands:

    py --version
    python3 --version

    Example output:

    Python 3.x.x

    Create a Project Folder

    First, create a separate folder for the Python project:

    mkdir virtual-environment-demo

    Move into the project folder:

    cd virtual-environment-demo

    Create a Virtual Environment

    Use the following command to create an environment named .venv:

    python -m venv .venv

    On Windows, the Python launcher may also be used:

    py -m venv .venv

    On Linux or macOS, you may need to use:

    python3 -m venv .venv
    .venv is a commonly used environment directory name. Other names such as venv or project-env may also be used.

    What Does venv Create?

    The venv module creates a directory containing the files needed for the isolated Python environment.

    Environment Item Purpose
    Python executable Runs Python using the selected environment.
    Activation scripts Configure the current shell to use the environment.
    Package directory Stores packages installed for the project.
    Configuration file Stores information about the virtual environment.