Python Virtual Environments and Pinned Dependencies

A virtual environment is an isolated folder of Python packages for one project. Without one, packages installed for one script can break another, and deployments become hard to reproduce.

Create and activate an environment on Linux or macOS:

bash python3 -m venv .venv source .venv/bin/activate python -m pip install --upgrade pip python -m pip install requests

On Windows PowerShell, activate with .venv\Scripts\Activate.ps1 instead.

To record exact versions, freeze the environment and reinstall from that file later:

bash python -m pip freeze requirements.txt python -m venv .venv2 .venv2/bin/python -m pip install -r requirements.txt

Frozen files include every transitive dependency, which makes builds repeatable. Many teams keep a short list of direct dependencies in a separate file and generate the pinned file with a tool such as pip-compile from the pip-tools package.

A few checks help before deploying:

- Run python -m pip check to find packages with conflicting requirements. - Use python -m pip list --outdated to see what can be upgraded. - Never install packages with sudo pip, because that can overwrite system tools.

Keep .venv/ in .gitignore so only the requirements file is committed.