Skip to content

Set up: Python, a project folder and a first script that runs

Everything you need to run every listing in the course: Python 3.9 or newer, a clean project folder, a check script that tests your install and speed, how to run files, and what to do when something fails.

12 min·Verified 2026-11+50 XP
Share

The first parts of the course need almost nothing: Python and a text editor. This lesson gets you to a working setup and a first script that runs.

#What you need

Need For Required?
Python 3.9 or newer Every listing Yes
A text editor (VS Code, anything) Writing files Yes
A terminal Running scripts Yes
A virtual environment Keeping packages tidy Optional for the plain-Python parts
PyTorch The mapping lesson at the end Optional, and it also works on a CPU
A GPU Nothing in this course requires one No

The plain-Python listings use only the standard library (math, random, collections, time), so there is nothing to install.

#1. Check Python

Open a terminal:

bash
python3 --version

You want 3.9 or newer. If the command is missing or older, install Python from python.org (or with your system's package manager) and open a new terminal. On Windows the command is often python or py instead of python3; use whichever works, and read python3 in the listings as that command.

#2. Make a project folder

bash
mkdir llm-from-scratch
cd llm-from-scratch

Put every listing from the course in this folder, in a file named in the lesson (for example bpe.py, attention.py, microgpt.py). Keeping them together means they can import each other when a lesson says so.

Optionally, create a virtual environment (you can skip this until the PyTorch lesson):

bash
python3 -m venv .venv
source .venv/bin/activate        # on Windows: .venv\Scripts\activate

#3. Run a first script

Save this as check.py:

python
import sys, time, math, random

print("Python", sys.version.split()[0])
assert sys.version_info >= (3, 9), "Please use Python 3.9 or newer"

random.seed(0)
t = time.time()
s = 0.0
for i in range(1_000_000):
    s += math.sqrt(i) * random.random()
dt = time.time() - t

print(f"1,000,000 loop steps took {dt:.2f} s")
print("Looks fine." if dt < 5 else "Slow machine: the labs will take a while, but they will still work.")

Run it:

bash
python3 check.py

You should see your Python version, a time (typically well under a second on a modern laptop, though it varies a lot between machines) and Looks fine.

The point of the timing: pure Python is slow on purpose, because nothing is hidden behind fast native libraries. The lab that trains the tiny GPT runs on one CPU core and took about 105 seconds on the author's laptop. If your machine is much slower, give it time.

#How to run a listing

  1. Copy the code into a file with the name given in the lesson.
  2. Run python3 filename.py from the project folder.
  3. Compare your output with the lesson's. Where a number depends on the random seed or on the Python version, the lesson says so.
  4. Change one thing, predict the result, run again.

#Reading error messages

Python errors read from the bottom up: the last line says what went wrong, and the lines above show where.

Message Usual cause Fix
python3: command not found Python not installed, or the command is python Install, or try python or py
SyntaxError A typo or a bad paste (missing bracket, wrong indentation) Look at the line shown; check the brackets and indentation just above it
IndentationError Mixed tabs and spaces Use four spaces everywhere
ModuleNotFoundError: No module named 'bpe' You are not in the project folder, or the file name is different cd into the folder; check the file is named exactly as the import
NameError A variable or function is used before it is defined Check the order and spelling
ZeroDivisionError An empty list or a zero count Print the values just before the failing line

Two habits save hours: print values while debugging (shapes, lengths, the first few numbers), and change one thing at a time.

#Optional: PyTorch later

You will only need it for the last lesson of the course. When you get there:

bash
pip install torch

The CPU version is enough. The lesson explains the mapping from your code to the library, so it makes sense even if you do not run it.

#Working with the code

  • Keep your files. Later lessons reuse earlier ones.
  • Use version control if you like (git init), and commit after each lesson that works.
  • Seeds: the listings call random.seed(...). Keep them while learning, so your numbers match the text; remove them later to see the natural variation.
  • Do not paste into a notebook cell unless you know how to rerun it cleanly; a notebook with stale state confuses beginners. A plain .py file is the reliable way.

#Exercise

  1. Run check.py. Write down your Python version and the loop time.
  2. Change the loop to 2,000,000 steps. Predict the new time before you run it (about twice as long). Was your prediction right?
  3. Remove the line random.seed(0) and run it three times. Which printed numbers change and which stay the same?

Answers: the time roughly doubles because the work doubles. Without the seed the time and the sum s are different on each run, while the Python version stays the same; the seed fixes the sequence of random numbers, which is why the course always sets one.

Next: the maths and Python you need.

Quick quiz

+10 XP per correct answer, +25 XP for a perfect score

  1. 1. Which software does the plain-Python part of this course need?

  2. 2. Why do we set random.seed(...) in the listings?

  3. 3. Your lab run takes 5 times longer than the lesson says. What is the most likely reason?

  4. 4. What does 'python3 -m venv .venv' create?

Found this useful?

Send it to someone who is learning the same thing.

Share this lesson