Skip to content

virtual environment

In Python, a virtual environment (or “venv”) is a directory tree that provides an isolated runtime environment on top of an existing Python installation. It has its own site-packages directory for installed packages, along with its own copy of, or symlink to, the Python executable.

This isolation allows you to maintain project-specific dependencies separately from the system-wide Python packages. It helps prevent conflicts between projects with different requirements.

By using virtual environments, you ensure that each of your Python projects has a dedicated and independent setup, which facilitates dependency management and helps you maintain your codebase.

Virtual environments are incredibly useful when working with multiple projects that require different versions of the same package or library. They also make your setup reproducible. You can record the exact dependencies that your project needs and recreate an equivalent environment on another machine without touching the global Python installation.

Example

You can create and activate a virtual environment using the venv module, which is included in the Python standard library. Here’s a quick example:

Language: Windows PowerShell
PS> python -m venv venv\
PS> venv\Scripts\Activate.ps1
(venv) PS>

Here, you create a new virtual environment in a venv\ folder by using the venv module. Then, you activate the virtual environment by executing venv\Scripts\Activate.ps1. The new prompt, (venv) PS>, indicates that you successfully activated the virtual environment.

Language: Shell
$ python -m venv venv/
$ source venv/bin/activate
(venv) $

Here, you create a new virtual environment in a venv/ folder by using the venv module. Then, you activate the virtual environment with the source command. The new prompt, (venv) $, indicates that you successfully activated the virtual environment.

Python Virtual Environments (venv)

Tutorial

Python Virtual Environments: A Primer

Create isolated project setups on all platforms, and gain a deep understanding of Python's virtual environments created with the venv module.

intermediate devops tools

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Sept. 22, 2026 • Reviewed by Dan Bader