Syllabus Lesson 45 of 244 · Files, Errors & Modules
Files, Errors & Modules

Packages, pip and Virtual Environments

The standard library is large, but the wider Python world has hundreds of thousands of extra packages on the Python Package Index (PyPI). You install them with pip, Python's package installer, run from your terminal (not inside a program):

pip install requests

Different projects often need different versions of the same package. A virtual environment (venv) is an isolated, per-project sandbox of installed packages so they never clash:

python -m venv .venv          # create it
source .venv/bin/activate     # turn it on (mac/linux)
pip install requests          # installs only inside this env

To record exactly what a project needs, freeze the list into a requirements.txt file so anyone can recreate it:

pip freeze > requirements.txt
pip install -r requirements.txt   # later, on another machine

A requirements line looks like requests==2.31.0: a package name, ==, then a version.

Here in the browser

This course runs on Pyodide, which has no terminal and no real pip. Its in-browser equivalent is micropip.install("name"). You will not need extra packages for this track, so instead of installing anything, the exercise below has you read a requirements.txt the way a tool does: parse each pinned line into a name and a version.

Your turn

Write parse_requirements(text) that turns the contents of a requirements.txt into a dict mapping each package name to its pinned version. Handle real files: skip blank lines and comment lines (starting with #), and ignore surrounding whitespace. Only handle exact name==version pins. For example parse_requirements("requests==2.31.0\n# core\nnumpy==2.0.0") is {"requests": "2.31.0", "numpy": "2.0.0"}.

Spotted a problem in this lesson? Report it

Code · runs in your browser
Output