The Isolation Bubble
Virtual environments: how to prevent Python projects from tearing each other apart.
You know how to install packages with pip. Everything works — until one day you update NumPy for a new project and your old TensorFlow project refuses to start. You didn't change a single line of code, but something invisible broke.
Welcome to Dependency Hell: the moment when shared package versions clash across projects. This article introduces two tools that prevent this nightmare — virtual environments for Python-level isolation and Docker containers for full system-level isolation.
Dependency Hell — When Packages Clash
Dependency Conflict
In a real workshop, you see the chaos immediately. In Python, conflicts hide until a runtime import crashes with a cryptic error message. Also, in a workshop you could theoretically have two versions of the same tool side by side — in one Python environment, only one version of numpy can be active.
Example: TensorFlow vs. NumPy
Three Levels of Isolation
Virtual Environments — One Bubble Per Project
Virtual Environment (venv)
The plastic-wrap analogy works well: isolation and easy cleanup. But a venv only isolates Python packages — not the operating system, system libraries, or GPU drivers. And requirements.txt is more precise than a wrap — it is an exact inventory with version numbers.
The venv Workflow in 5 Steps
Reproducibility with requirements.txt
Warning: Never pip install Without an Active venv
Interactive: Before vs. After
Toggle between the two scenarios: on the left, see how a global installation leads to version conflicts. On the right, the same workflow with virtual environments — each project gets its own bubble, no package interferes with any other. Pay attention to the error message on the left versus the clean training start on the right.
needs numpy<1.24
needs numpy>=2.0
Docker — The Shipping Container for Code
Docker
A shipping container has its own physical walls; Docker containers share the host's kernel — they are lighter but also more dependent. Cloning a physical container requires time; Docker images are copied and pushed to registries in seconds. And shipping containers don't start in seconds — Docker containers do because there is no boot process.
Isolates Python packages only. Lightweight, built into Python since 3.3, no extra tool needed. Ideal for everyday development. Does not isolate the operating system or system libraries.
Isolates the entire system: OS, libraries, GPU drivers, Python, and packages. Heavier, requires Docker installation. Ideal for deployment, collaboration, and specific GPU/CUDA setups.
Example: Minimal Dockerfile for an AI Project
Misconception: Docker Is a Virtual Machine
Deep Dive: requirements.txt Done Right
Deep Dive: Docker in the AI Ecosystem
Key Takeaways
Quiz: Virtual Environments & Docker
Checkpoint
- What happens when two projects need different versions of the same library and both are installed globally, without a virtual environment?
- What does the command python -m venv my_project do to your Python setup and package management?
- What is the main difference between a Python virtual environment and a Docker container in terms of what they isolate?