Lesson 10 of 20 · 48 min · UG / professional
Virtual environments and pip
The college PC has one global Python. Your project gets a private toolbox. pip install into that toolbox, then pin versions.
Application blueprint
A program is folders, config and a main door.
You will be able to
- ✓ Treat a venv as a private toolbox per project
- ✓ Install with pip into that toolbox, not the college global Python
- ✓ Pin what you installed so the next lab PC matches
The college PC has one global Python
Your project must not break the next student’s pandas. A virtual environment is a private toolbox: its own python, its own site-packages. python -m venv .venv then activate, then pip install requests. The OS Python stays boring. If you pip install into global because it was faster at 11 p.m., the next lab image will fight you — and so will every other course on that machine.
requirements.txt (or a lock file) is the list of what this project needs. Pin versions you tested. ‘pip install pandas’ with no version is how last year’s notebook dies on this year’s major release.
pip is not a language feature
import requests works after pip put the package into this environment. import is language; pip is packaging. Mixing them in your head is how people type pip in a Python file. Never do that. Two commands, two jobs.
This sandbox cannot create a venv. The walkthrough is the checklist as data: tools you would install, a function that refuses to ‘run’ if a required name is missing — the same decision as ImportError on a laptop.
Words that matter
- venv
- An isolated Python + libraries for one project.
- pip
- The installer that fills a venv from PyPI (or a wheelhouse).
- Pin
- Record the version you tested so the next machine can match.
Common mistakes
Avoid: pip install pandas on the college global interpreter.
Do this: Activate the project venv, then pip install, then freeze or pin.
Run it step by step
Each box is a real program. Press Run, change a number, Run again — the output must follow your code.
1. Step 1 — a toolbox as a list
This is your mental requirements file. One project, named tools.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — refuse to run without a tool
On a laptop this is ImportError. Here it is a named Reject.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
On a full Python install — venv + pip
Isolate requests/pandas so the lab image stays clean. Pin what you tested.
python -m venv .venv then .venv\Scripts\activate (Windows) then python -m pip install requests==2.32.3
Once activated, pip talks to this project only. Freeze the pins.
Real library code (not run in this browser sandbox)
# run in a terminal, not inside a .py file # python -m venv .venv # .venv\Scripts\activate # python -m pip install requests==2.32.3 # python -m pip freeze > requirements.txt import requests print(requests.__version__)
Example program — Pin as data
A version next to a name is the start of a lockfile.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — List the toolbox
Print a line that includes both pandas and numpy.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.