Virtual Environment
In one line
A virtual environment is an isolated Python environment with its own packages, separate from the system Python.
In simple words
A virtual environment (venv) is an isolated directory containing a Python installation and a set of installed packages. It lets you manage dependencies per project without conflicts.
Without virtual environments, all Python projects share the same system-wide packages. If project A needs Django 3 and project B needs Django 4, they conflict. Virtual environments solve this by giving each project its own package directory.
Create one with python -m venv myenv, activate it (source myenv/bin/activate on Unix, myenv\Scripts\activate on Windows), then install packages with pip install. The activated environment's packages are isolated from other projects.
Example
# Create a virtual environment
python -m venv myenv
# Activate (Unix/macOS)
source myenv/bin/activate
# Activate (Windows)
myenv\Scripts\activate
# Install packages (isolated to this env)
pip install requests flask
# Deactivate
deactivateVirtual environments isolate project dependencies. Create with venv, activate, install packages, deactivate when done.
Common confusions
Confused with: container
The difference: A virtual environment isolates Python packages only. A container (Docker) isolates the entire runtime environment including OS, libraries, and Python itself. Virtual environments are lighter; containers are more complete.
Related terms
Related Resources
Related Lessons
- Virtual Environments
Virtual environments.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.