Syllabus Lesson 45 of 239 · 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 the exercise below is just a light check that you understand the name-plus-version shape of a requirement.

Your turn

Write a function requirement_line(package, version) that returns a single requirements string in the form name==version. For example requirement_line("requests", "2.31.0") should return "requests==2.31.0".

Spotted a problem in this lesson? Report it

Code · runs in your browser
Output