Skip to content

What are Python virtual environments?

Updated Nov 30, 20263 min readBased on official documentation
What are Python virtual environments?

In short

A Python virtual environment is a self-contained folder with its own Python copy and its own set of installed packages. It lets each project use the library versions it needs without affecting other projects on the same computer. You create one with the built-in venv module and switch to it by activating it.

The problem they solve

Python projects often depend on extra packages, which are libraries written by other people and installed on top of Python. Those packages have versions, and versions change over time. A newer release may fix a bug, but it may also remove a feature that another project still uses.

Imagine two projects on one computer. Project A needs version 1.0 of a library. Project B needs version 2.0. If you install only one of them in the shared Python installation, the other project may stop working. Upgrading a library for one project can break the second project in the same way.

A virtual environment solves this. It is a self-contained folder that holds a Python installation and its own packages. Each project gets its own environment, so project A can keep version 1.0 while project B uses version 2.0. Upgrading a package in one environment does not touch the others.

How to create one

The standard tool for this is a module called venv, which comes with Python. It does not need to be installed separately. The Python version used to create the environment is the version that goes into it. If you run the command with python3.12, the environment gets Python 3.12.

Choose a folder for the environment and run venv with that folder name. The official tutorial uses this command:

python -m venv tutorial-env

The command creates the folder if it does not exist. Inside it, venv places a copy of the Python interpreter and some support files. A common name for the folder is .venv. The leading dot keeps it hidden in most shells, and the name makes its purpose clear. It also avoids confusion with .env files, which some tools use to store environment variable settings.

How to activate it

After you create the environment, you activate it. Activation changes your shell so that the python command points to the environment's copy of Python. Your prompt also changes to show the environment's name.

On Windows, run the activate script in the Scripts folder. On macOS or Linux, use the source command with the activate script in the bin folder. The tutorial shows the bash version of the script. If you use the csh or fish shells, use the matching activate.csh or activate.fish script instead.

To leave the environment, type deactivate in the terminal.

One detail is worth knowing. Activation does not change the PYTHONPATH variable. If PYTHONPATH points to code that is not compatible with the environment's Python version, you may see strange results. The tutorial recommends unsetting PYTHONPATH in your shell.

Installing packages with pip

Once an environment is active, you manage its packages with pip. pip is the package installer for Python. By default it downloads packages from the Python Package Index, a public collection of Python packages.

To install the latest version of a package, give its name:

python -m pip install requests

To install a specific version, add == and the version number. To upgrade to the newest release, use the --upgrade flag. To remove packages, use uninstall. The show subcommand displays details about one package, and list shows every package in the environment.

The freeze subcommand writes the installed packages and their versions in a format pip can read. Many teams save that output to a file named requirements.txt. The file can be stored in version control. Anyone who needs the project can then run pip install -r requirements.txt to recreate the same set of packages.

Common mistakes

A frequent mistake is installing packages before activating the environment. Those packages end up in the global Python installation instead. Check your prompt before you install anything.

Another mistake is committing the environment folder itself to version control. The folder is tied to one machine and one Python build. Commit requirements.txt instead, and let each developer create their own environment.

Finally, remember that an environment belongs to one project. If you switch projects, activate the other environment or deactivate the current one first.

Where this fits in web development

Web frameworks written in Python, such as Django or Flask, are usually installed into a virtual environment. Those frameworks serve pages over HTTP, the protocol browsers use to request web content. If you are new to that protocol, our guide on What is HTTP? explains the basics. Front-end code often runs in the browser, where the What is the DOM? page describes the structure the browser builds from a page.

Summary of the workflow

Create the environment once per project. Activate it whenever you work on that project. Install packages with pip while it is active. Save the package list with freeze. Deactivate when you finish. This small routine keeps your projects independent and makes them easier to share.

The official documentation at https://docs.python.org/3/tutorial/venv.html covers these steps in more detail, including the options for the venv module.

FAQ

Do I need to install venv separately?

No. venv is a module that ships with Python, so you can run it with python -m venv right after installing Python. The environment it creates uses the Python version you ran the command with.

Should I name my environment folder .venv or something else?

The tutorial suggests .venv because the leading dot keeps the folder hidden in most shells and the name explains its purpose. Any name works, but a consistent name across projects makes life easier.

Why does my terminal prompt show an environment name?

The prompt changes when the environment is active, so you can see which environment you are using. Run deactivate to return to your normal Python setup.

Can two projects share one virtual environment?

You can, but it defeats the main purpose. If they need different versions of the same package, they will conflict. Giving each project its own environment avoids that problem.

Sources

This page is based on the official documentation below. Read it for full detail.

Python virtual environments — official documentation ↗

Python virtual environments in the news

blozhikDeveloper news from across the web, explained. Every story links to its original sources.