Free — no signup required

Dependency Isolation: venv and pip

2 min read

The "Works on My Machine" Problem

Imagine you write a deployment script using boto3 version 1.26. It works perfectly on your laptop. Six months later, a colleague clones the repo and runs it on their machine, which has boto3 version 1.34 installed globally. The script crashes because a method signature changed between versions. Or worse: they install a new library for a different project, and it silently downgrades a shared dependency your script relies on, breaking it in a subtle way that only manifests at 2 AM during an incident.

This is dependency hell — and it is one of the most common sources of "it worked yesterday" failures in ops automation.

Virtual Environments: Isolated Toolboxes

Python solves this with virtual environments — isolated directories that contain their own Python interpreter and their own set of installed libraries, completely separate from the system-wide Python installation.

Think of it like having separate toolboxes for separate jobs. Your plumbing toolbox and your electrical toolbox don't share tools. If you upgrade a wrench in one box, the other box is unaffected.

Python's built-in venv module creates a virtual environment as an isolated directory with its own interpreter, activated by sourcing a script into the current shell so that any subsequent pip install lands inside that directory instead of the system-wide Python. Freezing the installed packages to a requirements file captures every library and its transitive dependencies pinned to exact versions — the contract that lets anyone who clones the repo recreate the identical environment by creating a fresh virtual environment and installing from that same file. This is reproducibility.

Poetry: The Modern Alternative

poetry is a third-party tool that goes further than venv + pip. It manages virtual environments automatically, resolves dependency conflicts intelligently, and separates "direct dependencies" (what your code needs) from "transitive dependencies" (what your dependencies need). It uses a pyproject.toml for human-readable dependency declarations and a poetry.lock file for exact pinned versions.

For team projects and anything going into production, poetry is the recommended approach. For quick one-off scripts, venv + pip is sufficient.

Golden Rule of Ops Python: Never install libraries into your system-wide global Python installation. Always use a virtual environment for every project. Your future self — and your colleagues — will thank you.

This is one of 18 chapters

Every chapter is free — Kubernetes, Terraform, SRE, distributed systems, and more — with fast daily review built in.

Sign up free