# Why should I use a virtual environment?

A virtual environment is an isolated Python runtime environment that allows you to work on different Python projects with different dependencies without conflicts. Virtual environments are fundamental to Python development.

## The Problem Virtual Environments Solve

Think of a virtual environment like a clean workshop for each project. Instead of having all your tools mixed together in one garage (which could lead to version conflicts or missing dependencies), each project gets its own dedicated space with exactly the tools it needs.
When you install Python packages globally (directly into your system Python), several problems arise:

- Version conflicts: Different projects might need different versions of the same package
- Dependency pollution: Installing packages globally makes it hard to track which packages are needed for each project
- System integrity: Modifying system Python packages can break system tools that depend on specific versions
- Reproducibility: Without isolation, it's difficult to ensure your project works the same way on different machines

Virtual environments solve these problems by creating isolated spaces where each project can have its own independent set of packages and Python version.

## How Virtual Environments Work

A virtual environment is a directory containing a Python installation or symlinks to one, along with a site-packages directory for installing dependencies. When activated, it modifies your shell's PATH to use this isolated environment instead of your system Python.

Virtual environments:

- Can use different Python versions
- Each has its own package registry (site-packages directory)
- Use symlinks instead of copying Python files for efficiency
- Can be easily created and destroyed without affecting other environments
- Can be recreated from requirement specifications

## Modern Virtual Environment Management

While tools like [`venv`](https://pydevtools.com/handbook/reference/venv.md) and [`virtualenv`](https://pydevtools.com/handbook/reference/virtualenv.md) have traditionally been used to create virtual environments, modern tools like [uv](https://pydevtools.com/handbook/reference/uv.md) handle virtual environment management automatically:

- uv creates environments on demand
- Dependencies are installed into the environment automatically
- Project environments are activated implicitly when running commands
- Environments are kept in sync with project requirements

{{< callout type="warning" >}}
Even with modern tools, understanding virtual environments conceptually helps debug issues and understand what's happening under the hood.
{{< /callout >}}

## Best Practices

When working with virtual environments:

- Use one environment per project: Don't share environments between projects
- Don't version control environments: Add `.venv` to your `.gitignore`
- Keep environments disposable: Be able to recreate them from requirements
- Use project-local environments: Store them in your project directory (typically as `.venv`)

## Learn More

- [What is a Virtual Environment?](https://pydevtools.com/handbook/explanation/what-is-a-virtual-environment.md)
- [venv](https://pydevtools.com/handbook/reference/venv.md) reference
- [virtualenv](https://pydevtools.com/handbook/reference/virtualenv.md) reference
- [How to customize uv's virtual environment location](https://pydevtools.com/handbook/how-to/how-to-customize-uvs-virtual-environment-location.md)
- [Why should I avoid using the system Python?](https://pydevtools.com/handbook/explanation/why-should-i-avoid-system-python.md)
