What is a Python virtual environment and do I need one?
A virtual environment is a per-project folder holding its own copy of Python and its own installed packages, so two projects can use different versions of the same library without conflict.
#Create and activate one
python -m venv .venv
# macOS / Linux
source .venv/bin/activate
# Windows PowerShell
.venv\Scripts\Activate.ps1Your prompt gains a (.venv) prefix. From now on pip install puts packages inside that folder rather than in your system Python.
deactivate leaves it.
#Why it matters
Without one, every pip install goes into a single global pile. Project A needs requests 2.25, project B needs 2.31, and only one of them can win. Virtual environments give each project its own pile.
They also mean you can delete .venv and rebuild from scratch when something breaks, which is much better than debugging a polluted global install.
#Record your dependencies
pip freeze > requirements.txt # save
pip install -r requirements.txt # restore, on another machine#Add it to .gitignore
.venv/The environment is rebuilt from requirements.txt; it does not belong in version control. It is also large and platform-specific.