1.4.10 · D3Python & Scientific Computing

Worked examples — Virtual environments and pip - conda

3,495 words16 min readBack to topic

You have read the parent note and you know what a virtual environment is: a self-contained folder with its own Python and its own site-packages/. This page does one thing — it walks you through every kind of situation you will actually hit, from the boring happy path to the weird degenerate cases nobody warns you about.

Before we start, one promise: every command here is explained in plain words the first time it appears. If you have never opened a terminal, you can still follow from line one.


The scenario matrix

Think of each row as a distinct case class. The worked examples below are labelled with the cell they cover, and together they touch every row.

# Case class The core question Degenerate / edge version
C1 Happy path Create → activate → install → freeze Empty requirements.txt (zero packages)
C2 Isolation proof Is python really the env's python? Two envs pointing at same Python version
C3 Version conflict Two packages want incompatible numpy Constraint that cannot be satisfied at all
C4 The leak bug A package "works" but isn't in the env pip freeze silently missing a package
C5 Reproduce elsewhere Recreate exact env on new machine Wrong Python version → install fails
C6 pip vs conda choice Which tool for a non-Python dependency? Package exists in neither (must build)
C7 Word problem Real team, real deadline Teammate on Windows (different activate)
C8 Exam twist Predict sys.path ordering Global + venv both have the package

The single idea that unlocks all of them:

Figure — Virtual environments and pip - conda

Figure s01 — reading it. This is a flow diagram, not a graph with numeric axes; the boxes are folders and commands, the arrows show which folder Python reaches first. On the left, the butter box is the python command. The lavender arrow (labelled "activated: look here 1st") goes up to the env's site-packages box, which contains requests, then out to the mint "FOUND" box — so when activated, the env folder is searched first and wins. The coral arrow (labelled "not activated: skip to global") goes down, past the env, straight to the pale global site-packages box and the "fallback" box. The vertical position encodes search order: higher = searched earlier. Every bug below is one of these two arrows pointing the "wrong" way.


Worked examples

C1 — The happy path (and its empty edge case)


C2 — Proving isolation actually happened

Connects to Python basicssys is a standard-library module you import like any other.


C3 — Version conflict (the classic, and the impossible case)


C4 — The leak bug (a package that "works" but isn't isolated)


C5 — Reproduce elsewhere (and the wrong-Python edge)


C6 — pip or conda? Choosing by dependency type


C7 — Word problem (real team, real deadline)


C8 — Exam twist: predict the winner in sys.path


Recall Quick self-test — cover the answers

Which folder does Python check first after you activate a venv? ::: The venv's site-packages, because activation puts it at the front of sys.path. A package runs in your env but pip freeze doesn't list it. Where does it actually live? ::: In the global site-packages — it leaked in via the search fall-through (case C4). Two packages need numpy>=1.20 and numpy<1.20. One env or two? ::: Two — the constraint intersection is empty, so no single version works (case C3). sys.executable prints /usr/bin/python3 after you "activated". What went wrong? ::: You did not actually activate this terminal; each terminal needs its own activation command. On Windows, what replaces source env/bin/activate? ::: env\Scripts\Activate.ps1 (PowerShell) or env\Scripts\activate.bat (cmd) — run without source. Why use conda over pip for MKL-optimized NumPy? ::: pip only installs Python code; MKL is a compiled binary library that conda bundles pre-built (case C6).