Deep Learning with PyTorch / Start Here

Set up a CPU PyTorch environment

Lesson 2 of 41. Open to read. No account needed.

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.

LibraryVersion
torch2.14.1, CPU build
torchvision0.29.1
NumPy2.5.4
Matplotlib3.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:

CommandmacOS and Linux (bash or zsh)Windows PowerShell
Show the current folderpwdpwd
List what is in itlsls
Enter a foldercd <folder>cd <folder>
Create a foldermkdir -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. Running Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser allows 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 with sudo apt install python3-venv (if the message names a versioned package such as python3.13-venv, install that one), then delete the half-made .venv folder 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

Four boxes from top to bottom: create and activate a virtual environment; step 1 installs NumPy 2.5.4 and Matplotlib 3.11.2 from PyPI; step 2 installs torch 2.14.1 and torchvision 0.29.1 from the PyTorch CPU index; confirm with pip check and the check script. An orange box warns that the reverse order makes the CPU index supply an older NumPy first.

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. The supported line compares the first two with (3, 12) and (3, 14) and rejects exactly 3.14.1. It must say True.
  • sys.executable is the interpreter running the script. It must be inside pytorch-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, +cpu after 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.cuda is the version of NVIDIA's CUDA toolkit that this torch was built with. None means a CPU build.
  • torch.ones(2, 2).device creates 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. False is 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

  1. Your prompt shows (.venv) but check_setup.py prints an interpreter path outside pytorch-practice. What does that suggest?
  2. Why is --index-url on the torch command and not on the NumPy command?
  3. A classmate on an Apple silicon Mac sees torch: 2.14.1 with 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

  1. 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.py in the activated terminal and compare.
  2. 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.
  3. No. macOS has only one build of torch 2.14.1 and it has no suffix. The CUDA line showing None confirms that no CUDA build is involved.

References

36 more lessons in the full course

Course access opens every lesson and adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 199 plus 18% GST. 90 days of online access. No auto-renewal.

Get the full course

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy