Set-up · 20 minutes
Set up your lab for How AI works
- Create a folder and a virtual environment for the course
- Install scikit-learn with pinned versions
- Prove with a check script that the lab works
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. Type the code yourself first: you learn more.
What you need
- A Linux, macOS or Windows computer with WSL (a Linux terminal inside Windows). You need no GPU, and on most systems no administrator rights.
- Python 3.12 or newer. The pinned libraries below need it. Check it in step 2.
- A text editor, and a terminal.
- About 500 MB of free disk space and an internet connection for the install. After the install, every lesson runs offline.
Everything runs on your own machine, in one folder: ~/krlab/how-ai-works. Nothing here touches a system that is not yours.
- requirements.txt → pip: lists four pinned versions
- pip → PyPI: asks for exactly those versions
- PyPI → .venv: pip stores the packages in the .venv folder
- labcheck.py → .venv: proves that the packages load
Set it up
1. Make the lab folder
mkdir -p (make directory, with its parents) creates the folder. cd (change directory) moves you into it.
mkdir -p ~/krlab/how-ai-works
cd ~/krlab/how-ai-works
pwd/home/you/krlab/how-ai-worksThe course page also offers a zip of every lab file. You do not need it to follow the lessons. If you want it, unzip it, then run sh kr-lab.sh <lesson folder> ~/krlab/how-ai-works: the script builds the lab folder as it stands after that lesson. The course page names the lesson folders.
2. Check your Python
python3 --version prints the version of Python.
python3 --versionPython 3.14.4If the number is below 3.12, install a newer Python first (see "If it fails").
3. Make a virtual environment
A virtual environment is a private folder of Python packages. The packages of this course stay in it, and they cannot break the Python of your system. python3 -m venv .venv creates it in the folder .venv. source .venv/bin/activate switches it on: from then on python and pip mean the ones in .venv, and your prompt starts with (.venv). pip is the tool that installs packages.
python3 -m venv .venv
source .venv/bin/activate
python --versionPython 3.14.4Run the source line at the start of every lesson. The command deactivate switches the environment off.
4. Pin the versions and install
A pinned version is an exact one, written with ==. Pinning matters because a model is numbers, and its numbers can change between versions of a library: a new default setting, a new way of adding numbers, a changed random generator. If everyone installs the same versions, everyone gets the same numbers, and the answers that the lessons check are the same for you as for the author. Unpinned, pip takes the newest version of today, and you may get a different last digit.
Create requirements.txt with an editor and put these four lines in it. In a terminal you can use nano requirements.txt: type the text, press Ctrl+O and Enter to save, then Ctrl+X to leave. They are the versions that the lessons were tested with.
requirements.txt
scikit-learn==1.9.1
numpy==2.5.5
scipy==1.18.1
joblib==1.6.0pip install -r requirements.txt installs everything in the file. (-r means "read the list from a file".) Scikit-learn is the machine-learning library of this course. NumPy and SciPy do the number work inside it, and joblib saves models. The install adds a few helper packages too, and it takes a minute or two.
pip install -r requirements.txtSuccessfully installed cloudpickle-3.1.3 joblib-1.6.0 narwhals-2.29.0 numpy-2.5.5 scikit-learn-1.9.1 scipy-1.18.1 threadpoolctl-3.8.0Check your lab
The script labcheck.py proves that the lab works. It checks the Python version, that you are inside the virtual environment, that scikit-learn, NumPy and joblib load with the pinned major and minor number (the first two parts of the version), and that a tiny model trains. It prints one line for each check, and a last line with the total. __import__(name) loads a library whose name is in a variable. __version__ is the version text that a library carries. sys.prefix is the folder of the Python that runs, and it differs from sys.base_prefix only inside a virtual environment.
You do not need to read this script line by line yet: lesson 1 explains the new Python words. In short, it runs each check, prints ok or FAIL, and counts the ok lines. Create labcheck.py.
labcheck.py
import sys
PINNED = {"sklearn": "1.9", "numpy": "2.5", "joblib": "1.6"}
results = []
def check(name, ok, detail):
results.append(ok)
print("ok " if ok else "FAIL", name.ljust(8), detail)
v = sys.version_info
check("python", v >= (3, 12), f"{v.major}.{v.minor}.{v.micro}")
check("venv", sys.prefix != sys.base_prefix, sys.prefix)
for module, want in PINNED.items():
try:
lib = __import__(module)
have = ".".join(lib.__version__.split(".")[:2])
check(module, have == want, f"{lib.__version__} (the lessons use {want}.x)")
except ImportError:
check(module, False, "not installed")
try:
from sklearn.linear_model import LogisticRegression
model = LogisticRegression().fit([[0], [1], [2], [3]], [0, 0, 1, 1])
check("model", list(model.predict([[0], [3]])) == [0, 1], "trained and predicted")
except Exception as error:
check("model", False, str(error))
print(f"lab ready: {sum(results)} of {len(results)}")python labcheck.pyok python 3.14.4
ok venv /home/you/krlab/how-ai-works/.venv
ok sklearn 1.9.1 (the lessons use 1.9.x)
ok numpy 2.5.5 (the lessons use 2.5.x)
ok joblib 1.6.0 (the lessons use 1.6.x)
ok model trained and predicted
lab ready: 6 of 6Your Python version and your folder can differ. Every other line must be the same. Type the last line of your own output and the version of scikit-learn that it printed.
If it fails
Read the first line that starts with FAIL. Fix that one, then run python labcheck.py again.
| The script says | What it means | What to do |
|---|---|---|
FAIL python | Python is older than 3.12. | Install Python 3.12 or newer from your system package manager or from python.org, then repeat steps 3 and 4 with that python3. This step needs the internet and your system's package manager, and it is outside this course. |
ensurepip is not available when you run python3 -m venv | Your system has Python without its venv part. | On Debian and Ubuntu install the package python3-venv with your package manager (this needs administrator rights and is outside this course), then repeat step 3. |
FAIL venv | You did not switch the environment on. | Run source .venv/bin/activate in the lab folder. The prompt must start with (.venv). |
FAIL sklearn not installed | The install did not run in this environment. | Switch the environment on, then run pip install -r requirements.txt again. |
FAIL sklearn 1.8.0 (the lessons use 1.9.x) | A different version is installed. | Run pip install -r requirements.txt again. It replaces the version with the pinned one. |
If pip says that it cannot find scikit-learn==1.9.1 (for example because your Python is too old for it, or your system has no wheel for it), do not give up. Change the four lines of requirements.txt to the newest versions that pip offers, and run the install again. The lessons should still work. Three things change:
- The check script prints
FAILfor the version lines. Ignore that. The linespython,venvandmodelmust still sayok. - The check on this page expects
lab ready: 6 of 6, so it will not accept your last line. - Some numbers can differ in the last digit, for example 0.9622 and 0.9618. If a number differs from yours only in the last digit, your lab is fine. If it differs in the first two digits, check your code.
Then record what you have: pip freeze > my-versions.txt. This file is the proof of the versions behind your numbers.