1.3.12 · D1Python Intermediate

Foundations — Virtual environments — venv, pip, requirements.txt

1,836 words8 min readBack to topic

This page assumes nothing. Before you touch the parent topic, every symbol, word, and squiggle it uses is built here from the ground up. Read top to bottom — each idea is a brick for the next.


0. What is a "terminal" and a "command"?

Before any Python, we need the place where all these commands happen.

The line you type into is called the prompt. It often ends in $ (Linux/Mac) or > (Windows). When you see a code block like:

python -m venv .venv

it means: type those words into the terminal and press Enter. The word bash just labels the language of the terminal — you don't type it.


1. python — the program that runs your code

Think of it as a translator you can call by name.


2. PATH — how the terminal finds python

Here is the secret gear that makes virtual environments work at all.

Recall Check yourself

If two folders on PATH both contain python, which runs? ::: The one in the folder listed first (higher up) on PATH.


3. Packages and site-packages — Python's toy box


4. pip — the package installer

Where does pip download from? From a giant online warehouse — see pip and PyPI for the full story. For now: pip = the delivery truck, site-packages = the shelf it stacks boxes on.


5. -m — "run a module by name"

The parent note insists on python -m venv, never bare venv. Let's earn that -m.

Recall Check yourself

Why is python3.11 -m venv .venv safer than venv .venv? ::: -m runs the venv tool belonging to that exact Python (3.11), so the env is guaranteed to be a 3.11 env — no guessing which venv PATH finds.


6. The moving parts of a venv folder


7. requirements.txt and version symbols ==, >=

Two symbols show up in it:

These are just the "equals" and "greater-or-equal" you know from arithmetic, applied to version numbers. Why version numbers have three parts like 3.0.3 is explained in Semantic versioning.


8. Where each install actually goes — sys.path

The full mechanics of how import walks this list live in Python import system and sys.path. For this topic, one fact matters: the active env's site-packages sits on sys.path, so import requests finds the env's copy first.


The prerequisite map

Everything above feeds the topic like this:

Terminal and commands

python interpreter

PATH search order

site-packages folder

pip installs packages

activate edits PATH

python -m venv creates env

requirements.txt shopping list

Virtual environment topic

Read it as: the terminal lets you run python; python plus -m venv builds the env folder; PATH order is what activation exploits; pip fills the env's site-packages; requirements.txt records what you filled it with. All arrows converge on the topic.


Where this leads

Once these bricks are solid, natural next steps in the vault are Dependency management — poetry, pipenv (smarter tools built on the same ideas), Reproducible builds and Docker (isolation taken further), and .gitignore best practices (why you never commit the .venv/ folder).


Equipment checklist

Self-test: can you answer each without peeking? If any stalls, reread that section.

What is a terminal, in one sentence?
A text window where you type one instruction, press Enter, and the computer runs it.
What does python refer to on your machine?
One specific program file that reads and runs .py code.
What is PATH and why does order matter?
An ordered list of folders the terminal searches for a command; it uses the FIRST match, so putting a folder on top overrides everything below.
What is site-packages?
The folder where installed packages are stored and where import looks for them.
What does pip install X do, and where does X land?
Downloads package X and drops it into the site-packages of whichever python is first on PATH.
Why write python -m venv instead of bare venv?
-m runs the venv tool of that exact python, pinning the env's interpreter version.
What lives inside a venv folder?
A pyvenv.cfg pointing to base Python, a bin//Scripts/ folder (env's python + pip), and an empty site-packages.
What does "activate" physically do?
Puts the env's bin//Scripts/ folder at the top of PATH for the current terminal only.
What is requirements.txt?
A text file listing package names and versions so the env can be rebuilt anywhere.
Difference between ==3.0.3 and >=3.0?
== means exactly that version; >= means that version or any newer one.