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
Definition
Dependency Conflict
Analogy
Definition
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
You try to install TensorFlow 2.12 and a newer NumPy version:
pip install tensorflow==2.12.0
pip install numpy==1.24.2
ERROR: tensorflow 2.12.0 requires numpy<1.24,>=1.22,
but you have numpy 1.24.2 which is incompatible.
Successfully installed numpy-1.24.2The catch: pip only complains, but installs the new NumPy anyway, and TensorFlow now runs with a version it was never released for. Even worse: Project A uses TensorFlow with numpy==1.23. Then you install PyTorch for Project B with numpy==2.1. The global installation overwrites Project A's version. Next time Project A starts: crash — "A module that was compiled using NumPy 1.x cannot be run in NumPy 2.1.0 as it may crash."
Three Levels of Isolation
Virtual Environments — One Bubble Per Project
Definition
Virtual Environment (venv)
Analogy
Definition
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.
Note
The venv Workflow in 5 Steps
- 1. python -m venv ai_project — Creates the environment directory
- 2. source ai_project/bin/activate (Linux/macOS) or ai_project\Scripts\activate (Windows) — Activates it (prompt now starts with (ai_project))
- 3. pip install transformers — Installed only in this environment
- 4. pip freeze > requirements.txt — Saves exact versions
- 5. deactivate — Returns to global Python
Note
Reproducibility with requirements.txt
pip freeze outputs an exact list of all installed packages with version numbers:
pip freeze > requirements.txt
# Contents of requirements.txt:
numpy==1.26.3
tokenizers==0.15.0
transformers==4.37.0
...Another developer can use pip install -r requirements.txt to install exactly the same package versions on their machine. This is how reproducibility works in Python projects.
Caution
Warning: Never pip install Without an Active venv
Without an active virtual environment, pip installs globally — every package goes into the shared site-packages and affects all projects. On Debian and Ubuntu, pip even refuses to do this (error message externally-managed-environment), so there you need venv from the start. Before every pip install, check that your prompt shows (ai_project). When in doubt: which python (Unix) or where python (Windows) — the path should point to your venv directory.
Interactive: Before vs. After
Toggle between the two scenarios: the first shows how a global installation leads to version conflicts. The second shows the same workflow with virtual environments — each project gets its own bubble, no package interferes with any other. Pay attention to the error message in the first scenario versus the clean training start in the second.
needs numpy<1.24
needs numpy>=2.0
Docker — The Shipping Container for Code
Definition
Docker
Analogy
Definition
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, CUDA toolkit, Python, and packages. Heavier, requires Docker installation. Ideal for deployment, collaboration, and specific GPU/CUDA setups.
Example: Minimal Dockerfile for an AI Project
FROM python:3.11
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "train.py"]Two commands to build and run — works identically on any machine with Docker:
docker build -t my_ai_project .
docker run --rm my_ai_projectCaution
Misconception: Docker Is a Virtual Machine
Docker containers share the host system's kernel — they do not boot their own operating system. That is why they start in seconds instead of minutes. They are much lighter than VMs. (On Windows and macOS, Docker Desktop starts a lightweight Linux VM once in the background, and the containers then run inside it.) And Docker does not replace venv: in practice, both are often combined — Docker for the OS level, venv for the Python level inside the container.
There is an important difference between pinned and unpinned versions:
# Pinned (reproducible):
numpy==1.26.3
transformers==4.37.0
# Unpinned (NOT reproducible!):
numpy
transformersUnpinned means: "install the latest version." What is latest today is different tomorrow. pip freeze captures all versions exactly, including transitive dependencies. For cleaner files, you can alternatively maintain only top-level packages by hand — but always with versions. Newer tools like uv handle the environment, installation and lock file in one go; and with pylock.toml (PEP 751, since 2025) there is now a standard format for lock files, too.
In AI production, containers are the standard. Why? Because CUDA/cuDNN versions must match exactly — containers ship CUDA and cuDNN in matching versions; only the GPU driver has to be installed on the host.
Hugging Face offers Docker Spaces for deploying any framework. Amazon SageMaker AI provides pre-built containers for TensorFlow and PyTorch, and supports custom ones. GPU-accelerated containers with NVIDIA CUDA are the most common way to make deep learning setups reproducible.
When you build and deploy AI models in later articles, Docker will be your constant companion.
Note
Key Takeaways
- Dependency Hell happens when two projects need different versions of the same package but share one global Python installation. The solution: give each project its own environment.
- python -m venv project_name creates an isolated Python environment. source project_name/bin/activate (Linux/macOS) or project_name\Scripts\activate (Windows) switches into it.
- pip freeze > requirements.txt saves exact package versions. pip install -r requirements.txt reproduces the environment on another machine. Always pin versions.
- A virtual environment isolates Python packages only. Docker isolates the entire system: OS, libraries, CUDA toolkit, Python, and packages — all in one portable container.
- Use venv for everyday development. Use Docker when you need full reproducibility across machines, deploy to servers, or require specific GPU/CUDA setups.
Check
Quiz: Virtual Environments & Docker
What is "Dependency Hell"?
Select one answer
1. What is "Dependency Hell"?
2. Which commands create and activate a virtual environment?
3. Your colleague needs to use the exact same package versions. What do you share?
4. When should you use Docker instead of just a virtual environment?
Check
Checkpoint
Can you explain this in your own words? Tick what you've got.
0 of 3 done