# What is a Virtual Environment?

A virtual environment is an isolated Python runtime environment that enables development with project-specific dependencies and Python versions without interference from other projects or the system Python installation.

{{< callout type="info" >}}
Think of a virtual environment like a clean workshop for each project. Instead of having all your tools mixed in one garage (which could lead to version conflicts or missing dependencies), each project gets its own dedicated space with precisely the tools it needs.
{{< /callout >}}

Without virtual environments, several critical problems emerge when installing Python packages globally:

* Version Conflicts: Different projects often require different versions of the same package
* Dependency Pollution: Global package installation makes tracking project-specific requirements difficult
* System Integrity Risk: Modifying system Python packages can break system tools that depend on specific versions
* Poor Reproducibility: Lack of isolation makes it challenging to ensure consistent behavior across different machines

A virtual environment consists primarily of:

* A Python interpreter (or symlinks to one)
* A dedicated `site-packages` directory for installing dependencies
* Activation scripts that modify shell environment variables
* Configuration files specifying the environment's properties

## How activation works

Creating a virtual environment does not change anything by itself. Activating it rewrites the shell's `PATH` so that the environment's `python` and `pip` take precedence over the system installation. The command depends on the platform and shell:

```bash
source .venv/bin/activate
```
```powershell
.venv\Scripts\Activate.ps1
```
After activation, the shell prompt usually changes to show the environment name in parentheses, and running [`pip`](https://pydevtools.com/handbook/reference/pip.md) or `python` invokes the environment's copies. Running `deactivate` restores the original shell.

## What isolation looks like in practice

A short console session makes the abstract idea of isolation concrete. On macOS or Linux:

```console
$ which python
/usr/bin/python

$ python -m venv .venv
$ source .venv/bin/activate

(.venv) $ which python
/path/to/project/.venv/bin/python

(.venv) $ pip install cowsay
(.venv) $ python -c "import cowsay; print(cowsay.__file__)"
/path/to/project/.venv/lib/python3.14/site-packages/cowsay/__init__.py

(.venv) $ deactivate
$ python -c "import cowsay"
ModuleNotFoundError: No module named 'cowsay'
```

Inside the environment, `cowsay` imports from a file inside `.venv/`. Outside, the import fails entirely. Nothing was written to the system Python, and a second project can maintain its own independent set of packages without touching this one.

## Modern Virtual Environment Management

While traditional tools like `venv` and `virtualenv` require manual environment creation and activation, modern tools like `uv` handle virtual environments automatically:

* Creates environments on demand
* Installs dependencies into the environment automatically
* Activates project environments implicitly when running commands
* Maintains environment synchronization with project requirements

Traditional approach: three commands plus an explicit activation step.

```bash
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python main.py
```

Modern approach with [uv](https://pydevtools.com/handbook/reference/uv.md): one command that creates the environment, installs dependencies from `pyproject.toml`, and runs the script.

```bash
uv run main.py
```

## Learn More

* [How to create and use a Python virtual environment with venv](https://pydevtools.com/handbook/how-to/how-to-create-and-use-a-python-virtual-environment-with-venv.md) walks through the create / activate / install / deactivate lifecycle step by step.
* [Why should I use a virtual environment?](https://pydevtools.com/handbook/explanation/why-should-i-use-a-virtual-environment.md)
* [How to require a virtualenv when installing packages with pip](https://pydevtools.com/handbook/how-to/how-to-require-a-virtual-for-installing-packages.md)
* [How to customize uv's virtual environment location](https://pydevtools.com/handbook/how-to/how-to-customize-uvs-virtual-environment-location.md)
* [venv](https://pydevtools.com/handbook/reference/venv.md) reference
* [virtualenv](https://pydevtools.com/handbook/reference/virtualenv.md) reference
