What is a virtual environment (python -m venv .venv) for?
Options
- Running Python code inside the browser without installing it
- An isolated set of packages per project, so versions do not conflict (Correct option)
- A faster interpreter that compiles your code to machine code
- A Docker container that holds the whole project and its OS
Answer
An isolated set of installed packages per project, so projects do not conflict on library versions
Why
Project A may need Django 4 while project B needs Django 5. If both install into the same global Python, they clash.
A virtual environment gives each project its own folder of packages. After activating it, pip install installs only there.
List the versions in requirements.txt or pyproject.toml so others can recreate it.
Remember
One virtual environment per project.