Kahibaro
Discord Login Register

1.3 Installing Python and PyTorch

What you are installing and why

To run deep learning code with PyTorch, you need a working Python installation, a way to create isolated environments for projects, and the PyTorch packages themselves. The goal of this chapter is to get you to a point where you can open a terminal, run python, import torch, and confirm that PyTorch is using the hardware you expect.

Rule: Use one environment per course or project. Do not install PyTorch into your system Python if you can avoid it.

Install Python

Install a recent, stable Python version. For beginners, Python 3.10 or 3.11 is a safe choice for PyTorch compatibility and tutorials. Download it from https://www.python.org/downloads/ and install it.

On Windows, make sure the installer option to add Python to PATH is enabled. After installation, open a new terminal and verify:

bash
python --version

If python is not found on Windows, try:

bash
py --version

Create an isolated environment

You need an environment so your dependencies do not conflict with other projects. The simplest built in option is venv.

Choose a folder for your course work, then create and activate an environment.

On macOS or Linux:

bash
python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip

On Windows PowerShell:

powershell
python -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install --upgrade pip

If PowerShell blocks activation, you can temporarily allow it for the current session:

powershell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process

Rule: Every time you start working, activate the environment first. If you forget, packages may install to the wrong place and imports will fail.

Install PyTorch

Install PyTorch inside the activated environment. The exact command depends on whether you want CPU only or GPU support. The most reliable way is to copy the command from https://pytorch.org/get-started/locally/ because it reflects current wheels for your operating system and accelerator.

A common CPU only install is:

bash
pip install torch torchvision torchaudio

If you have an NVIDIA GPU and want CUDA support, use the command provided by the PyTorch site for your CUDA version. You typically install via pip, and PyTorch brings the matching CUDA runtime libraries with it, so you usually do not need to install CUDA separately for beginners.

If you are on Apple Silicon, you can still use pip installs, and PyTorch can use the Metal backend when available.

Verify the installation

Create a file named check_torch.py and run it.

python
import torch
print("torch version:", torch.__version__)
print("python ok")
print("cuda available:", torch.cuda.is_available())
if torch.cuda.is_available():
    print("cuda device:", torch.cuda.get_device_name(0))
print("mps available:", hasattr(torch.backends, "mps") and torch.backends.mps.is_available())

Run:

bash
python check_torch.py

You should see the PyTorch version and whether CUDA or MPS is available. It is normal for cuda available to be False on machines without an NVIDIA GPU.

Rule: If import torch fails, confirm you are in the correct environment and that pip belongs to that same environment by running python -m pip --version.

Common installation issues and quick fixes

If pip install succeeds but your editor cannot import torch, your editor is likely using a different Python interpreter than your terminal. Select the interpreter that points to your .venv.

If you see an error about a missing compiler or build tools, you may be installing a package from source. For PyTorch itself, you should be installing prebuilt wheels. Recheck that you used an official PyTorch install command and that your Python version is supported.

If you previously installed another torch package or have conflicting environments, start fresh by deleting the .venv folder and recreating it, then reinstalling.

Minimal “it works” test in the Python REPL

After activation, you can also test quickly in an interactive session:

bash
python

Then:

python
import torch
x = torch.tensor([1.0, 2.0, 3.0])
print(x * 2)

If this runs without errors, you are ready to move on to using tools like notebooks and editors, and to start working with tensors in PyTorch.

Views: 64

Comments

Please login to add a comment.

Don't have an account? Register now!