Lesson · Virtual Environments, Dependencies & Packaging
Virtual Environments
65 min readVirtual Environments
A virtual environment gives a project an isolated Python environment for installed packages. It prevents unrelated projects from accidentally depending on the same mutable site-packages directory.
Learning goals
- Create a project environment with
python -m venv .venvand explain what the new directory contains. - Activate that environment in a shell (
source .venv/bin/activateor.venv\Scripts\Activate.ps1) and describe what activation changes about the namespythonandpip. - Run a script through the environment's own interpreter, without activation, by calling its path.
- Explain why two projects sharing one
site-packagesdirectory interfere, and how one environment per project prevents it. - Describe which facts about an environment must be written down before another machine can rebuild it.
Mental model
An environment is a directory holding an interpreter and its own site-packages, and activation is only a PATH edit that makes the current shell prefer that interpreter. The isolation is about where packages get installed, not about a special kind of Python.
Mental model
read it top to bottom
shell asks for "python" PATH decides which executable answers ↓ .venv is activated or the interpreter is called by full path the project interpreter runs its site-packages belongs to this project only ↓ pip installs into it packages land beside that interpreter a recorded set of versions what another machine would rebuild from
Core idea
A virtual environment turns a project's dependencies into a fact about its directory instead of a fact about the machine. Everything the tooling does (activation, pip targeting, interpreter selection) follows from one arrangement: each project gets its own site-packages, so installing a package in one project cannot silently upgrade or shadow another project's dependency. Reproducibility then comes from writing the versions down, which is what the rest of this module builds on.
Why isolation matters
Why isolation matters is a design question rather than a syntax rule. Use the mental model from this lesson to identify the conditions, tradeoffs, and failure modes that make one choice appropriate.
venv
venv creates an isolated environment containing interpreter-facing configuration and a site-packages location. Activate it for interactive convenience, but scripts can also call its interpreter directly.
Activating environments
In Virtual Environments, Activating environments names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
Installing packages
In Virtual Environments, Installing packages names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
Reproducibility
Reproducibility means another environment can rebuild an equivalent dependency set and run the same project. Record interpreter constraints and dependency versions at the appropriate project layer.
Walkthrough
python -m venv .venvcreates the directory.venvcontaining a Python interpreter and its ownsite-packages; run it in the project root so the environment sits next to the code it serves.- The two activation lines are alternatives for the same step:
source .venv/bin/activateon macOS and Linux,.venv\Scripts\Activate.ps1in Windows PowerShell; both edit the shell's PATH so the plain namespythonandpipresolve inside the environment. python -m pip install --upgrade pipis the first command the workflow runs after activation, and the-mform ties the tool to the interpreter the shell now resolves, so the upgrade lands inside.venvrather than on any system Python.- Predict before running: which executable
where python(Windows) orwhich python(macOS and Linux) reports before activation, and what it reports after.
Experiments to try
- Create the environment and compare
where python(orwhich python) before and after activation; the two paths should differ only by the.venvsegment. - Run a script twice, once with the environment activated and once as
.venv\Scripts\python.exe app.py(or.venv/bin/python app.py), and confirm the same interpreter answers in both cases. - Install one small package inside the environment, then deactivate and check that importing it with the system interpreter now fails.
- Delete the
.venvdirectory, recreate it with the samepython -m venv .venv, and note which things came back (the interpreter) and which did not (the packages), because nothing recorded them.
Common mistakes
- Installing before activating: a bare
pip installtargets whichever interpreter the shell resolves, which can be the system Python or another project's environment. - Committing
.venvto version control: the directory holds machine-specific binaries and absolute paths, so it is rebuilt from a recorded dependency list rather than shared. - Treating activation as installation: it only edits PATH for the current shell session, installs nothing, and does not follow the project into other terminals or into the editor.
- Letting two projects share one environment: the shared
site-packagesrecreates the original conflict, since one project's upgrade can change what the other imports.
Practice: Easy
- Create a scratch folder, run
python -m venv .venvthere, and open the directory tree it produced; name three things inside it and what each is for. - Activate the environment and print
sys.executable; deactivate and print it again. Diff the two paths and say which mechanism picked each one. - Run
.venv\Scripts\python.exe -c "import sys; print(sys.prefix)"(or thebin/pythonform) without activating, and confirm the prefix points inside.venv.
Practice: Medium
- Build two environments in sibling folders and install different versions of one package into each; prove with
import importlib.metadata; print(importlib.metadata.version("name"))that each reports its own version. - Write a fresh-clone checklist (create environment, activate, upgrade pip, install dependencies) as specific commands, then execute it in a new folder and fix every step that fails.
- Open the project in an editor that points at a different interpreter, describe how the mismatch surfaces (flagged imports, failing runs), then repoint the editor at
.venvand confirm the symptoms disappear.
Practice: Hard
- Design an environment policy for a team: the directory convention, the version-control ignore rules, the file that records interpreter constraints and pins, and the exact commands a new machine runs; justify each choice from the failure modes in this lesson.
- Build a failure lab: install a package before creating the environment, reuse an environment after upgrading the base interpreter, and clone a repository that committed its
.venv; reproduce each situation and explain the mechanism. - Measure two paths to a working environment: patch an existing one versus delete and rebuild from the recorded list. Time both and decide from the numbers when each is the right move.
Lesson checklist
- I can create an environment with
python -m venv .venvand say what the directory contains. - I can activate it in my shell and show that
pythonandpipnow resolve inside it. - I can run a script through the environment's interpreter path without activating anything.
- I can explain why
.venvis rebuilt rather than committed, and where version pins belong.
Remember
An environment is a directory that gives one project its own interpreter and `site-packages`, and activation is just a PATH edit in the current shell. That single arrangement prevents cross-project interference, and the discipline it asks for is small: one environment per project, install only through it, ignore it in version control, and write the versions down so it can be rebuilt.
Interview angles
What is the main purpose of **Why isolation matters**?+
Why isolation matters is a design question rather than a syntax rule. Use the mental model from this lesson to identify the conditions, tradeoffs, and failure modes that make one choice appropriate.
What should you pay attention to when using **venv**?+
venv creates an isolated environment containing interpreter-facing configuration and a site-packages location. Activate it for interactive convenience, but scripts can also call its interpreter directly.
How does **Activating environments** fit into this lesson?+
In Virtual Environments, Activating environments names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
What goes wrong when two projects share one environment, and what is the standard fix?+
Both projects resolve the same site-packages, so one project's install or upgrade can change what the other imports; the fix is one environment per project, rebuilt from that project's own recorded dependency list.