Back to section lessons
18 min · intro · Lesson 1 of 8

Python for AI: your environment and first function

Set up an isolated CPU environment, check your Python, NumPy and PyTorch versions, and write a diagnostic function.

Lesson contents
  1. 1 What you'll build
  2. 2 Download the course project
  3. 3 Why use a separate environment
  4. 4 Installing Python 3.15.1
  5. 5 macOS on Apple Silicon
  6. 6 Linux and Windows
  7. 7 Your first diagnostic check
  8. 8 Task
  9. 9 Common mistake

What you'll build

In this course, you'll build a local classifier for short incident descriptions. It will read a text such as "response time increased after a release" and choose one of four categories: availability, performance, connectivity or capacity.

The computer requirements and the full learning path are on the program page. Open the project in a separate terminal so you can run commands alongside the lesson.

All exercises run on the CPU. The course's baseline environment uses Python 3.15.1, NumPy 2.5.4, PyTorch 2.15.0 and pytest 9.2.0. You'll need two CPU cores, 8 GB of RAM and internet access for the initial dependency installation.

Download the course project

Download the Python for AI project (.zip)

Extract the archive and open a terminal in the folder that now contains course-projects. Run cd course-projects/python-for-ai from that folder.

Why use a separate environment

A virtual environment keeps the project's libraries separate from your system Python. Updating PyTorch in the course project won't affect other programs. Deleting the .venv directory removes its installed dependencies completely.

Work from the project directory:

Hover over a line or click it

cd course-projects/python-for-ai
Line explanations
  1. Line 1. cd changes the terminal's current directory. Here, it moves into the course project folder; run the following commands from there. The path is relative to the folder where you extracted the archive.

Don't replace the system Python or run pip with sudo. All commands below install packages inside .venv.

Installing Python 3.15.1

If python3.15 or py -3.15 isn't available yet, download CPython 3.15.1 from the official release page. For macOS, choose the installer. On Windows, use the 64-bit installer and install Python Launcher. After installation, open a new terminal and check the version.

If you already have uv, it can download a standard CPython build and create the project environment directly:

Hover over a line or click it

uv python install 3.15.1
uv venv --python 3.15.1 --seed .venv
Line explanations
  1. Line 1. uv is a separate program for managing Python and packages. This command downloads Python 3.15.1 if that version is not already installed through uv. Internet access is required.
  2. Line 2. Creates a virtual environment in .venv using Python 3.15.1. --python selects the version, while --seed adds pip, the library installer. This command does not activate the environment in your terminal.

After uv venv, activate .venv using the command for your OS, then continue with python -m pip install .... You don't need to create the environment again with python -m venv.

macOS on Apple Silicon

The tested combination requires macOS 14 or later and ARM64 architecture. First, check your machine:

Hover over a line or click it

uname -m
sw_vers -productVersion
python3.15 --version
Line explanations
  1. Line 1. uname prints system information; -m selects the machine architecture. This installation option expects arm64, which means an Apple Silicon processor.
  2. Line 2. sw_vers is a macOS system command. -productVersion prints the version number, such as 14.7. This course environment requires version 14 or later.
  3. Line 3. Runs the installed Python 3.15 and asks it to print its version instead of executing a program. This environment expects Python 3.15.1. A "command not found" error means the terminal could not find that command.

Expect arm64, macOS 14 or later, and Python 3.15.1. Then create the environment:

Hover over a line or click it

python3.15 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -e '.[dev]'
python -m pip check
Line explanations
  1. Line 1. Python runs its venv module using -m. The module creates the .venv folder with a separate project environment; your system Python libraries stay in place.
  2. Line 2. source reads the activation file in the current terminal. Afterward, python selects the Python in .venv. Activate the environment again in each new terminal window.
  3. Line 3. Runs pip through the active Python. install --upgrade pip updates pip itself inside the environment so it can install packages for your selected Python version.
  4. Line 4. Installs the project from the current folder, represented by the dot. -e lets you use source changes without reinstalling. [dev] adds development and testing tools from pyproject.toml. Quotes protect the square brackets from shell interpretation.
  5. Line 5. Checks that the installed libraries satisfy their declared requirements. No broken requirements found means pip found no dependency conflicts; this command does not check your code.

This environment has been tested on macOS ARM64. Intel macOS is outside the current support matrix.

Linux and Windows

Linux x64 requires glibc 2.28 or later. Check its version with ldd --version. Create the environment as follows:

Hover over a line or click it

python3.15 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install 'torch==2.15.0+cpu' --extra-index-url https://download.pytorch.org/whl/cpu
python -m pip install -e '.[dev]'
python -m pip check
Line explanations
  1. Line 1. Runs the built-in venv module with -m and creates a separate environment in .venv. It uses the installed Python 3.15; project libraries are kept separate from system libraries.
  2. Line 2. Activates the environment in the current Linux terminal. After activation, python and pip come from .venv. Activation does not carry over to other terminal windows.
  3. Line 3. Updates pip in the active environment. Running it through python -m links pip to the Python that will run your course code.
  4. Line 4. Installs PyTorch: the package name is torch, and == pins version 2.15.0+cpu. The +cpu suffix selects the CPU build without CUDA. --extra-index-url adds the official CPU package index alongside the regular pip index. Downloading requires internet access.
  5. Line 5. Installs the current project along with the [dev] development dependencies listed in pyproject.toml. The dot means the current folder. -e links the installation to the source, so your edits are used immediately.
  6. Line 6. Checks the requirements of installed libraries. If there are no conflicts, it prints No broken requirements found. Check course program errors separately with tests.

In Windows PowerShell, use Python Launcher and the Windows activation script:

Hover over a line or click it

py -3.15 -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
python -m pip install "torch==2.15.0+cpu" --extra-index-url https://download.pytorch.org/whl/cpu
python -m pip install -e ".[dev]"
python -m pip check
Line explanations
  1. Line 1. py is the Windows program for choosing an installed Python. -3.15 selects Python 3.15, while -m venv runs the module that creates a separate environment in .venv.
  2. Line 2. Runs the environment activation script in the current PowerShell. The leading .\ means the current folder. After activation, python should use .venv. If PowerShell blocks scripts, activation will fail.
  3. Line 3. Updates pip, the library installer, inside the active environment. python -m pip ensures installation uses the same Python that runs your code.
  4. Line 4. Installs PyTorch as the torch package at version 2.15.0+cpu. == pins the version; +cpu selects the CPU build without CUDA. --extra-index-url adds the official CPU package index alongside the regular pip index.
  5. Line 5. Installs the project from the current folder, represented by the dot. -e makes source changes available immediately. [dev] adds development and testing dependencies from pyproject.toml.
  6. Line 6. Checks installed libraries against their dependency requirements. It prints No broken requirements found when there are no conflicts. This check does not run the course tasks.

Compatible CPU wheels have been confirmed for Linux and Windows. Full course runs on these platforms are handled separately, so identical behavior is not yet claimed.

Your first diagnostic check

The course module provides a doctor command. It prints library versions and the compute device:

Hover over a line or click it

python -m incident_classifier doctor
Line explanations
  1. Line 1. Runs the incident_classifier course module through the active Python. doctor is its diagnostic command: it prints Python and library versions, plus the compute device. For this course, the device should be cpu.

In this course, the device should always be cpu. No GPU is required. Computations use float32 and two CPU threads.

Declare a Python function with def, put its arguments in parentheses, and return a result with return:

Hover over a line or click it

def package_name(name: str) -> str:
    return name.strip().lower()
Line explanations
  1. Line 1. def declares a function named package_name. It takes a value called name; : str describes the expected string, and -> str describes a string result. These annotations do not enforce types themselves. The colon starts the function body, whose lines are indented.
  2. Line 2. strip() removes whitespace from the edges of name, while lower() converts letters to lowercase. return sends the result back to the caller. For example, package_name(" Torch ") returns "torch"; the original string is unchanged.

The name: str annotation describes the expected argument type. -> str indicates the return type, but Python itself doesn't prevent you from passing a different value. We'll add type checks and clear errors in the next lesson.

Task

Open exercises/lesson01.py and implement this function:

Hover over a line or click it

def environment_summary() -> dict:
    ...
Line explanations
  1. Line 1. Declares environment_summary without arguments: the parentheses are empty. -> dict describes the expected dictionary result, a collection of key-value pairs. Here, the keys are library names and the values are their versions.
  2. Line 2. The three dots mark where your implementation belongs. They do not collect versions or return a dictionary. Replace this line with code that reads versions from the environment and returns them using return.

Return a dictionary with four keys:

Read the versions from the running environment. Don't hard-code them as string constants.

Hints

You can get the Python version from the platform module. NumPy and PyTorch publish their versions in the __version__ attribute.

Collect the four values in separate variables first. Then return a dictionary with the required keys.

Check

Hover over a line or click it

python -m pytest -q tests/test_exercises.py --student exercises -k lesson01
Line explanations
  1. Line 1. Runs pytest, the code testing tool, through the active Python. tests/test_exercises.py contains the exercise checks. -q shortens the report, --student exercises selects your solutions, and -k lesson01 keeps only the first lesson checks. A passing result means these selected checks succeeded.

Before you implement the function, the test should fail. After you fix it, the test for lesson01 should pass.

Where to find the solution

The reference is in solutions/lesson01.py. Check your own function before comparing: it should read versions at runtime and always return the four required keys.

You can test the reference separately:

Hover over a line or click it

python -m pytest -q tests/test_exercises.py --student solutions -k lesson01
Line explanations
  1. Line 1. Checks the first lesson reference with pytest. -q shortens the output, --student solutions selects the reference folder instead of your exercises folder, and -k lesson01 keeps the first lesson. Success here does not mean your own solution passed.

Common mistake

pip install may use a different Python from the python command. python -m pip ties installation to the active interpreter and removes this ambiguity.

Done reading?

Mark the lesson as read to move your track progress forward.

Practice

1 free local lab with automated checks and hints from the agent.

Try for free