Set-up · 15 minutes
Set up your Python lab
- Create the lab folder and a virtual environment for the course
- Activate the environment and prove that it is active
- Run a check script that confirms the lab is ready
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 machine or a Linux shell that you own. A laptop, a virtual machine and Windows with WSL all work. You need a terminal and no administrator rights.
- Python 3.12 or later. Step 1 shows your version. Older versions are at or near the end of their security fixes (Python 3.10 reached its end of life in October 2026), and a security lab should run a version that still gets them. The outputs in this course come from Python 3.14, so a line can differ a little on 3.12.
- A text editor.
nanois on most systems. Any editor that saves plain text is fine. - About 50 MB of free disk space.
- The program
curl, for lesson 6. Typecurl --versionto see whether you have it. - An internet connection, for lesson 7 only.
Every task of this course runs on your own machine, in the folder ~/krlab/python. Nothing here touches a system that is not yours. If you do not yet know the terminal, do Part 1 of the Linux course first (lesson 1, "Find your way in the file system").
Set it up
1. Check your Python
python3 --version prints the version of the Python interpreter, the program that runs your scripts.
python3 --versionPython 3.14.4Your number can be different. If it is lower than 3.12, install a newer Python with your package manager before you go on. A Python that no longer gets security fixes is a risk in a security lab.
2. Make the lab folder
mkdir -p (make directory, with its parents) creates the folder and gives no error if it exists. cd (change directory) moves you into it.
mkdir -p ~/krlab/python
cd ~/krlab/python3. Create a virtual environment
A virtual environment (venv) is a private folder. It points to one Python and holds its own installed packages. The option -m venv runs the venv module of the standard library, and .venv is the name of the new folder.
python3 -m venv .venv
ls .venvbin
include
lib
lib64
pyvenv.cfgIf this fails on Debian or Ubuntu with a message about ensurepip, install the package python3-venv with your package manager, then run the command again.
4. Activate it
source runs a file in your current shell. The file .venv/bin/activate puts .venv/bin first in your PATH (the list of folders where the shell looks for a program when you type its name), so the word python now means the Python of the venv. Your prompt usually shows (.venv) after this.
source .venv/bin/activate
which python/home/you/krlab/python/.venv/bin/pythonYou must activate again in every new terminal. The command deactivate leaves the environment.
5. Write the check script
Create labcheck.py with your editor (for example nano labcheck.py) and put this in it. Do not study this script. It only checks your lab. Copy it, save it and run it.
labcheck.py
import importlib
import sys
from pathlib import Path
NEEDED = ["argparse", "csv", "difflib", "hashlib", "http.server", "ipaddress",
"json", "logging", "pathlib", "random", "re", "urllib.request"]
def modules_ok():
for name in NEEDED:
try:
importlib.import_module(name)
except ImportError:
return False
return True
checks = [
("Python 3.12 or later", sys.version_info >= (3, 12)),
("virtual environment is active", sys.prefix != sys.base_prefix),
(f"{len(NEEDED)} standard modules import", modules_ok()),
("folder ~/krlab/python exists", (Path.home() / "krlab" / "python").is_dir()),
]
passed = 0
for text, good in checks:
print("ok " if good else "FAIL", text)
passed += good
print(f"lab ready: {passed} of {len(checks)}")Check your lab
Run the script with the venv active.
python labcheck.pyok Python 3.12 or later
ok virtual environment is active
ok 12 standard modules import
ok folder ~/krlab/python exists
lab ready: 4 of 4The script prints one line for each requirement and a last line with the score. This check only confirms the format of that line: type it here.
If it fails
| What you see | Cause and fix |
|---|---|
python3: command not found | Python is not installed. Install the package python3 with your package manager. |
FAIL Python 3.12 or later | The interpreter is too old. Install a newer one and make the venv again with python3.12 -m venv .venv (use your version). |
FAIL virtual environment is active | You started the script with the system Python. Run source .venv/bin/activate in ~/krlab/python, then run python labcheck.py. |
FAIL folder ~/krlab/python exists | Run step 2 again. Check for a typo in the path. |
FAIL 12 standard modules import | A Debian-based system can ship a Python without some modules. Install the full package (python3-full) and make the venv again. |
The virtual environment was not created successfully | Install python3-venv, delete the .venv folder with rm -r .venv, and run step 3 again. |
No such file or directory for labcheck.py | You are in the wrong folder. Run cd ~/krlab/python and ls. |