← Knowledge Base
PythonReference note1 min read · updated 2026-11-27

Python virtual environments and pip

Create, activate and reproduce an isolated environment, and why you should not pip install globally.

pythonvenvpip
python3 -m venv .venv
source .venv/bin/activate            # Windows: .venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
pip install requests beautifulsoup4
pip freeze > requirements.txt        # record exact versions
pip install -r requirements.txt      # recreate elsewhere
deactivate

A virtual environment keeps each project's packages separate, so upgrades in one project cannot break another and system Python stays clean. On Debian/Ubuntu you may need sudo apt install python3-venv; recent distributions refuse global pip install (use a venv or pipx for command-line tools). Commit requirements.txt, not the .venv folder.