1.4.10 · D1Python & Scientific Computing

Foundations — Virtual environments and pip - conda

2,152 words10 min readBack to topic

This page builds up every word and symbol the parent 1.4.10 note uses, starting from things a curious 12-year-old already knows: files, folders, and typing commands. Nothing below assumes you have seen a terminal before.


1. A file, a folder, and a path

Before any Python, we need the most basic object: a path.

Figure — Virtual environments and pip - conda

What to look for in the figure: every rounded box is a folder, and the thin lines are the parent→child links. The deepest boxes (bottom row) hold actual package files. Notice the green branch (system packages) and the orange branch (venv packages) never touch — that separation is isolation. When the parent note says a venv is a "self-contained directory tree", it means exactly one of these branches, kept apart from the others.

Why do we need this word first? Because isolation — the whole point of the topic — is nothing more than "keep packages in separate branches of this tree so they never touch."


2. import — asking Python for a package

When you write:

import numpy

you are handing Python a name (numpy) and saying "go find the folder called this and load it." But Python does not search the whole disk — that would be slow. It only looks in a short, ordered list of folders.


3. sys.path — the ordered list of search folders

Here is the single most important symbol in the whole topic.

import sys
print(sys.path)
# ['', '/usr/lib/python3.10', '/usr/lib/python3.10/site-packages', ...]
Figure — Virtual environments and pip - conda

What to look for in the figure: read it like a checklist Python runs top to bottom. The first row is the '' current-folder entry. The blue arrow on the left shows the search direction. Python finds numpy in the highlighted green row, loads it, and the red "stop!" arrow shows it never looks at the rows below. This "first match wins" rule is the mechanism behind everything:


4. site-packages/ — where installed packages live

So the two symbols connect:

  • site-packages/ = where packages physically sit.
  • sys.path = the list that includes that folder so Python knows to look there.

A virtual environment gives you a brand-new empty site-packages/ and puts it near the top of sys.path. That is the entire trick — a fresh drawer, checked first.


5. .dist-info/ and metadata — the version label

When a package is installed, alongside its code goes a small folder ending in .dist-info/.

The == you keep seeing is not maths equality — it is a filter:


6. venv — the built-in tool that makes an environment

Before we edit any lists, we need the tool that creates the isolated folder.


7. $PATH and $VIRTUAL_ENV — the shell's version of sys.path

There are two search lists in play, and mixing them up causes most confusion. sys.path is for Python imports. $PATH is for which program the word python even runs.

Figure — Virtual environments and pip - conda

What to look for in the figure: the top block is $PATH before activation — python is found in a system folder. The bottom block is after activation — the orange venv/bin folder has been slipped in at the front, so now python resolves there first. It is the same "first match wins" rule as sys.path, just for programs instead of imports. When you activate an environment, the activate script runs:

export PATH="/path/to/venv/bin:$PATH"
export VIRTUAL_ENV="/path/to/venv"
  • export = "make this variable visible to programs I run."
  • Putting venv/bin in front means the word python now finds venv/bin/python first.
  • $VIRTUAL_ENV is a signpost variable recording which env is active (used to show the (venv) prompt).

8. Activate / deactivate — flipping the switch


9. pip vs conda — the two installers, in one line each

You now hold every symbol needed to read the parent note end to end.


Prerequisite map

The diagram below is written in Mermaid, a plain-text way to draw diagrams. Read it as a set of boxes joined by arrows: each box is a concept, and an arrow A --> B means "A must be understood before B." Follow the arrows top to bottom — everything funnels into the final "Virtual environment isolation" box.

Files and folders = a path

import asks for a package

sys.path = ordered search list

site-packages holds installed code

dist-info metadata and version equals

shell PATH picks which python runs

venv module creates the environment

activate prepends venv folder

Virtual environment isolation

pip and conda install into it

Related building blocks: reading/writing files like requirements.txt, packages such as NumPy you will install, and why MKL-optimized builds matter.


Equipment checklist

Test yourself — cover the right side and answer aloud.

What is a path?
The written address of a file or folder, steps separated by a separator (/ on Linux/Mac, \ on Windows).
What does import numpy make Python do?
Search sys.path top-to-bottom for a folder named numpy and load the first match.
What is sys.path?
The ordered list of folders Python searches on every import; first match wins.
What does the empty string '' mean at the front of sys.path?
The current working directory — the folder you launched python from, checked before everything else.
What is site-packages/?
The folder where pip/conda physically drop installed package files.
What command creates a virtual environment, and where does the tool come from?
python -m venv myenv; venv is a module built into Python 3.3+, no install needed.
What does the rule "first match wins" imply if a folder is at the top of the list?
Its packages are found before all others, so a venv's folder overrides system packages.
What does numpy==1.24.0 mean, and is it maths equality?
"Install exactly version 1.24.0" — it is a version filter, not equality.
When do two version specifiers create a conflict?
When their allowed ranges never overlap (e.g. >=1.20 and <1.20), so no single version satisfies both.
What is $PATH and how does it differ from sys.path?
$PATH is the shell's list for finding programs like python; sys.path is Python's list for finding imports. Both use first-match-wins.
What does the activate script actually change?
It prepends venv/bin to $PATH and sets $VIRTUAL_ENV, for the current terminal only.
Why must you re-activate in every new terminal?
Activation edits variables in one shell session's memory; a new terminal starts with system defaults.
One-line difference between pip and conda?
pip installs Python-only packages from PyPI; conda also installs the Python interpreter and non-Python binaries.