What You Will Set Up
Every practical lesson in this course runs Python scripts that use four
libraries: PyTorch (torch) for tensors and networks, torchvision for
image datasets and image transforms, NumPy for arrays, and Matplotlib
for charts. In this lesson you install exact versions of all four into a
virtual environment: a folder holding its own Python interpreter and
libraries, separate from any other Python on your computer. Exact versions
matter because defaults, warnings and printed results change between
releases, and the lessons describe these versions.
| Library | Version |
|---|---|
| torch | 2.14.1, CPU build |
| torchvision | 0.29.1 |
| NumPy | 2.5.4 |
| Matplotlib | 3.11.2 |
These were the current releases when the course was written (S01, S03, S07, S08).
By the end, a check script will confirm the versions, show that PyTorch is the CPU build, and show where new tensors are created. You will also be able to recognise the three most common installation mistakes from their output.
You need about 1.5 GB of free disk space for the environment (it uses about 1.1 GB) and an internet connection for the downloads. Installation takes one to three minutes on a typical connection.
Terminal Bridge
Every command in this course is typed into a terminal. Four commands are enough to move around:
| Command | macOS and Linux (bash or zsh) | Windows PowerShell |
|---|---|---|
| Show the current folder | pwd | pwd |
| List what is in it | ls | ls |
| Enter a folder | cd <folder> | cd <folder> |
| Create a folder | mkdir -p <folder> | mkdir <folder> |
~ means your home folder on macOS and Linux; PowerShell uses $HOME. So
~/pytorch-practice and $HOME\pytorch-practice are the same place on each
system. On Windows, use PowerShell, not the older Command Prompt: the
commands in this course are written for PowerShell.
Check Your Python Version
The course supports Python 3.12, 3.13 and 3.14, except the single release 3.14.1. The lower limit comes from NumPy 2.5.4, which requires Python 3.12 or later (S07). The upper limit comes from PyTorch 2.14.1, whose standard downloads are built for Python up to 3.14 (S01). torchvision 0.29.1 excludes 3.14.1 by name in its requirements (S03).
On macOS and Linux:
python3 --version
On Windows, the Python launcher lists every version you have installed:
py --list
If you have no supported version, install one from python.org on Windows and macOS, or from your Linux distribution's packages. On macOS, PyTorch 2.14.1 runs only on Apple silicon with macOS 14 or later; Intel Macs have no build of this version (S01, S02). Python 3.13 receives security fixes until about October 2029 (S14); 3.12 until about October 2028 (S96) and 3.14 until about October 2030 (S15).
A Python Readiness Check
The course assumes you can write and debug small Python programs. This check takes a few minutes and needs only Python, not the libraries.
Create the practice folder and the exercise folder. macOS and Linux:
mkdir -p ~/pytorch-practice/course-environment
cd ~/pytorch-practice/course-environment
Windows PowerShell:
mkdir $HOME\pytorch-practice\course-environment
cd $HOME\pytorch-practice\course-environment
Save this as readiness.py in that folder:
"""Python readiness check: finish average(), then fix the error that follows."""
def average(values):
# Return the mean of a list of numbers.
pass
scores = [72, 85, 90]
print("Average:", average(scores))
labels = {"cat": 0, "dog": 1}
print("Label for dog:", labels["Dog"])
Run it with python3 readiness.py (macOS and Linux) or py readiness.py
(Windows). It prints Average: None and then stops with a traceback whose
last line is KeyError: 'Dog'.
Your task: make average return the mean of its list, and fix the error
using the traceback's last line and the line it points to. When both work,
the script prints Average: 82.33333333333333 and Label for dog: 1. If this
took you more than a few minutes, practise Python before continuing; the
answer is at the end of this lesson.
Create the Environment
Go to the practice folder and create the environment in a folder named
.venv. macOS and Linux:
cd ~/pytorch-practice
python3 -m venv .venv
source .venv/bin/activate
Windows PowerShell (replace 3.13 with the supported version you
installed):
cd $HOME\pytorch-practice
py -3.13 -m venv .venv
.venv\Scripts\Activate.ps1
python3 -m venv .venv runs Python's built-in venv module, which creates
the .venv folder containing a copy of the interpreter and its own empty
library folder (S16).
Activating it changes your terminal session so that python and pip mean
the environment's copies; your prompt then starts with (.venv). Activation
lasts only for that terminal window: activate again in every new
terminal before running course scripts. deactivate switches back.
Two problems are common here:
- Windows: "running scripts is disabled on this system". PowerShell
blocks local scripts, including
Activate.ps1, by default. RunningSet-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserallows scripts created on your computer to run, while scripts downloaded from the internet must still be signed. It changes this setting for your user account only (S16). Then activate again. - Debian or Ubuntu: "ensurepip is not available". These distributions
package
venv's installer separately. Install it withsudo apt install python3-venv(if the message names a versioned package such aspython3.13-venv, install that one), then delete the half-made.venvfolder and create it again (S95). This needs administrator rights.
Install the Libraries in Two Steps
With the environment active, run these three commands. They are the same on every system:
python -m pip install numpy==2.5.4 matplotlib==3.11.2
python -m pip install torch==2.14.1 torchvision==0.29.1 --index-url https://download.pytorch.org/whl/cpu
python -m pip check

Install order. Read from top to bottom; the orange box explains why the order matters.
Why python -m pip. It runs the pip that belongs to the active
python, so packages cannot land in another Python by mistake.
Why two steps. == pins an exact version. Step 1 installs NumPy and
Matplotlib from PyPI, the standard Python package index. Step 2 installs
PyTorch and torchvision from a different index, given by --index-url: the
PyTorch CPU index, which holds builds without GPU support
(S02,
S04,
S06). This matters most on
Linux, where the ordinary PyPI download of torch is a GPU build of 555 MB that
also pulls in large NVIDIA libraries; the CPU build is 196 MB
(S01,
S02). On Windows both indexes
offer a CPU build, and on macOS there is only one build, which the CPU index
serves too. Using the CPU index everywhere keeps one procedure for every
system.
Why this order. torchvision depends on NumPy. Installed first, NumPy 2.5.4 already satisfies that requirement, so step 2 changes nothing about it. In the reverse order, the CPU index supplies its own older NumPy, which then has to be replaced. Each command uses one index, so pip never mixes packages from two indexes in one decision.
What pip check does. It confirms that every installed package's
requirements are met. Success prints No broken requirements found.
Check the Setup
Save this as check_setup.py in ~/pytorch-practice/course-environment:
"""Check the course environment: Python, libraries, build and device."""
import sys
import matplotlib
import numpy
import torch
import torchvision
version = sys.version_info[:3]
supported = (3, 12) <= version[:2] <= (3, 14) and version != (3, 14, 1)
print("Python:", ".".join(map(str, version)), "| supported:", supported)
print("Interpreter:", sys.executable)
print("torch:", torch.__version__)
print("torchvision:", torchvision.__version__)
print("NumPy:", numpy.__version__)
print("Matplotlib:", matplotlib.__version__)
print("CUDA version in this torch build:", torch.version.cuda)
print("A new tensor is created on:", torch.ones(2, 2).device)
print("Accelerator available:", torch.accelerator.is_available())
Run it with the environment active:
cd ~/pytorch-practice/course-environment
python check_setup.py
On Windows: cd $HOME\pytorch-practice\course-environment, then the same
python check_setup.py. Output on Linux (illustrative: your Python version
and folder differ):
Python: 3.13.15 | supported: True
Interpreter: /home/you/pytorch-practice/.venv/bin/python
torch: 2.14.1+cpu
torchvision: 0.29.1+cpu
NumPy: 2.5.4
Matplotlib: 3.11.2
CUDA version in this torch build: None
A new tensor is created on: cpu
Accelerator available: False
What Each Line Tells You
sys.version_info[:3]is Python's version as three numbers. Thesupportedline compares the first two with(3, 12)and(3, 14)and rejects exactly3.14.1. It must sayTrue.sys.executableis the interpreter running the script. It must be insidepytorch-practice/.venv(on Windows, ending in.venv\Scripts\python.exe). Anything else means the environment is not active.- The four versions must match the table at the top. On Linux and
Windows,
+cpuafter torch's and torchvision's versions shows that they came from the CPU index. On macOS the versions have no suffix; that is normal there. torch.version.cudais the version of NVIDIA's CUDA toolkit that this torch was built with.Nonemeans a CPU build.torch.ones(2, 2).devicecreates a small tensor and reports where it lives. PyTorch creates tensors in the computer's main memory,cpu, unless told otherwise.torch.accelerator.is_available()reports whether a GPU-like device is usable.Falseis expected here; the core course never needs one.
When the Output Looks Different
These outputs are illustrative. Each points to one cause.
ModuleNotFoundError: No module named 'matplotlib' (or 'torch'). The
script ran in a Python without the libraries. The usual cause is a new
terminal where the environment is not active, so python means your
system's Python. Check the prompt for (.venv), activate, and run again.
On Linux, the torch line has no +cpu and the CUDA line shows a version
number instead of None. torch came from PyPI without --index-url, so
you have the GPU build. Remove it with
python -m pip uninstall torch torchvision, then rerun step 2 exactly as
written. The download size is also a clue: hundreds of megabytes of packages
named nvidia-....
ERROR: Package 'torchvision' requires a different Python: 3.14.1 not in '!=3.14.1,>=3.10'.
The environment was created with Python 3.14.1, the one excluded release.
Install another supported version, delete .venv, and create it again with
that version. A message such as No matching distribution found for torch==2.14.1 usually means a Python version or system that this release
does not support at all, such as an Intel Mac.
No space left on device or Disk quota exceeded. The disk or your
storage allowance is full. Free about 1.5 GB, delete the partly installed
.venv, and start again from "Create the Environment".
Your Turn
Open a new terminal window. Without activating the environment, run (macOS and Linux):
python3 -c "import torch"
On Windows, run py -c "import torch". Predict what happens, then activate
the environment, rerun check_setup.py, and compare.
Clean Up
Keep ~/pytorch-practice/.venv for the whole course. Remove only this
exercise's folder, after checking that you are in the practice folder and
the folder name is right. macOS and Linux:
cd ~/pytorch-practice
ls
rm -r course-environment
ls
Windows PowerShell:
cd $HOME\pytorch-practice
ls
Remove-Item -Recurse course-environment
ls
The first ls shows course-environment (and .venv, which ls hides on
macOS and Linux because its name starts with a dot). The second shows that
course-environment is gone.
Check Your Understanding
- Your prompt shows
(.venv)butcheck_setup.pyprints an interpreter path outsidepytorch-practice. What does that suggest? - Why is
--index-urlon the torch command and not on the NumPy command? - A classmate on an Apple silicon Mac sees
torch: 2.14.1with no+cpu. Is their installation wrong?
Answer and Reasoning
Readiness check
Replace pass with return sum(values) / len(values): sum adds the
numbers and len counts them. Then change labels["Dog"] to
labels["dog"]. Dictionary keys match exactly, including capital letters,
and the traceback's last line, KeyError: 'Dog', names the key that was not
found. The script then prints Average: 82.33333333333333 and
Label for dog: 1.
Your turn
Before activation, python3 is your system's Python, which does not have the
course libraries, so the command fails with
ModuleNotFoundError: No module named 'torch'. (If your system Python
happens to have its own torch, the import works, but check_setup.py would
then show a different interpreter path and possibly different versions.)
After activation, check_setup.py prints the expected output again. The
lesson: each terminal needs its own activation.
Understanding check
- Something other than the activated environment's interpreter ran the
script, for example a full path to another Python, an editor's "Run"
button set to another interpreter, or a shell alias. Run
python check_setup.pyin the activated terminal and compare. - torch and torchvision need the CPU builds, which come from the PyTorch CPU index. NumPy and Matplotlib come from PyPI. Keeping one index per command makes it clear where each package came from.
- No. macOS has only one build of torch 2.14.1 and it has no suffix. The
CUDA line showing
Noneconfirms that no CUDA build is involved.
References
- S01: PyPI: torch 2.14.1
- S02: PyTorch CPU wheel index: torch
- S03: PyPI: torchvision 0.29.1
- S04: PyTorch CPU wheel index: torchvision
- S06: PyTorch: Start Locally
- S07: PyPI: NumPy 2.5.4
- S08: PyPI: Matplotlib 3.11.2
- S14: PEP 719: Python 3.13 Release Schedule
- S15: PEP 745: Python 3.14 Release Schedule
- S16: Python 3.13: venv
- S95: Python Packaging User Guide: Installing with Linux package managers
- S96: PEP 693: Python 3.12 Release Schedule