Skip to main content
PyTorch Essentials
CHAPTER 02 Intermediate

Setting Up Python and PyTorch Environment

Updated: May 16, 2026
7 min read

# CHAPTER 2

Setting Up Python and PyTorch Environment

1. Introduction

Deep Learning is computationally intense. To build models in PyTorch, your computer needs the right software stack. Because PyTorch relies heavily on underlying C++ libraries and GPU hardware accelerations (CUDA), installing it correctly is crucial. The phrase "It works on my machine" is a nightmare in AI development. In this chapter, we will walk through a bulletproof setup process to ensure your Deep Learning environment is stable, reproducible, and ready for massive datasets.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Install a compatible version of Python.
  • Create and manage Python Virtual Environments.
  • Navigate the PyTorch installation matrix.
  • Install PyTorch using pip with or without GPU support.
  • Set up Visual Studio Code (VS Code) and Jupyter Notebooks.

3. Installing Python

PyTorch requires a 64-bit version of Python. *Warning: Do not install the very latest version of Python the day it comes out. PyTorch takes a few weeks/months to release compatible binary files. Stick to stable versions like Python 3.9, 3.10, or 3.11.*

Windows:

  1. 1. Go to python.org/downloads and download the Windows installer.
  1. 2. CRITICAL STEP: On the very first screen of the installer, check the box "Add Python to PATH". If you skip this, nothing will work in your terminal.

macOS:

  1. 1. Open terminal and install Homebrew: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  1. 2. Run: brew install python

Linux (Ubuntu): sudo apt update sudo apt install python3 python3-pip python3-venv

4. Virtual Environments

Never install PyTorch globally on your main computer system. It will inevitably conflict with other Python tools. We use a Virtual Environment (an isolated sandbox folder).
  1. 1. Open your terminal/command prompt.
  1. 2. Create a folder for this course: mkdir pytorchcourse and enter it: cd pytorchcourse
  1. 3. Create the virtual environment (named ptenv):
  • Windows: python -m venv ptenv
  • Mac/Linux: python3 -m venv ptenv
  1. 4. Activate the environment:
  • Windows: ptenv\Scripts\activate
  • Mac/Linux: source ptenv/bin/activate
*(You should now see (ptenv) at the start of your terminal line).*

5. Installing PyTorch

Unlike normal Python libraries, PyTorch comes in many different "flavors" depending on your computer's hardware (specifically, your GPU).

Go to the official website: pytorch.org/get-started/locally/ You will see an installation matrix. Select:

  • PyTorch Build: Stable
  • Your OS: Windows / Mac / Linux
  • Package: Pip
  • Language: Python
  • Compute Platform:
  • If you have an NVIDIA GPU, select CUDA 11.8 or CUDA 12.1 (This downloads the GPU version).
  • If you have a Mac, or a standard laptop with no NVIDIA graphics card, select CPU.

The website will generate a command for you. It will look something like this:

bash
12
# Example for CPU only
pip install torch torchvision torchaudio

6. VS Code and Jupyter Notebook Setup

Data Scientists write exploratory code in Jupyter Notebooks, which allow you to run code in blocks and see images/graphs instantly.
  1. 1. Download Visual Studio Code from code.visualstudio.com.
  1. 2. Open VS Code, go to the Extensions tab (squares on the left menu).
  1. 3. Search for and install the Python extension and the Jupyter extension.
  1. 4. Open your pytorchcourse folder in VS Code.
  1. 5. Create a new file named hellopytorch.ipynb.
  1. 6. Open the file, click "Select Kernel" in the top right, and choose your pt_env environment.

7. Step-by-Step Implementation: Verify Installation

Let's ensure PyTorch is installed and test if it detects a GPU. Type this into the first cell of your Notebook and click Play (Shift+Enter):
python
12345678910111213141516
import torch

print(f"PyTorch Version: {torch.__version__}")

# Check if CUDA (NVIDIA GPU) is available
if torch.cuda.is_available():
    print("SUCCESS: NVIDIA GPU detected! Training will be fast.")
# Check for Apple Silicon GPU (M1/M2/M3 Mac)
elif torch.backends.mps.is_available():
    print("SUCCESS: Apple Metal GPU detected!")
else:
    print("NOTICE: No GPU detected. PyTorch will run on the CPU.")

# Do some basic math
tensor = torch.tensor([10, 20, 30])
print(f"Tensor multiplied by 2: {tensor * 2}")

8. CUDA and GPU Basics (Advanced)

Training a Deep Learning model involves millions of matrix multiplications.
  • CPU: Has a few very powerful cores (4 to 16). Great for sequential tasks.
  • GPU (Graphics Card): Has thousands of weaker cores. Great for doing thousands of simple math problems simultaneously.
PyTorch relies on CUDA, a software layer created by NVIDIA. If you installed the CUDA version of PyTorch, PyTorch handles all the complex C++ drivers for you. You don't need to manually install the massive NVIDIA CUDA Toolkit anymore!

9. Common Mistakes

  • Installing the wrong Compute Platform: If you have an NVIDIA GPU but accidentally select the "CPU" version on the PyTorch website, PyTorch will install successfully, but torch.cuda.isavailable() will return False. You must uninstall it and install the CUDA version.
  • Forgetting to activate the environment: If you open a new terminal tomorrow and type import torch, it will fail unless you run ptenv\Scripts\activate first.

10. Best Practices

  • Google Colab: If you have a terrible laptop and cannot install PyTorch, or you don't have a GPU, use Google Colab (colab.research.google.com). It is a free Jupyter Notebook in the cloud that comes with PyTorch pre-installed and gives you free access to an NVIDIA GPU!

11. Exercises

  1. 1. Follow the matrix on the PyTorch website and install PyTorch into your virtual environment.
  1. 2. Run the verification script to check your version and GPU status.

12. MCQ Quiz with Answers

Question 1

Why must you use a Virtual Environment when installing PyTorch?

Question 2

What is the name of the NVIDIA software layer that PyTorch uses to accelerate math on the GPU?

13. Interview Questions

  • Q: Explain why a GPU trains neural networks significantly faster than a high-end CPU.
  • Q: What is the purpose of the PyTorch installation matrix on their official website?

14. FAQs

Q: I have an AMD Graphics Card. Can I use it? A: Support for AMD cards in PyTorch uses a platform called ROCm. While it is fully supported on Linux, setting it up on Windows is currently very difficult. AMD Windows users are highly encouraged to use Google Colab for GPU training.

15. Summary

Setting up your environment correctly is a rite of passage in Data Science. By leveraging virtual environments, carefully utilizing the pip installation matrix, and verifying our hardware acceleration, we have built a stable foundation. You are now equipped with the same software stack used by AI engineers at Meta and Tesla.

16. Next Chapter Recommendation

Before we dive into Tensors and complex calculus, we must ensure our foundational coding skills are sharp. In Chapter 3: Python Basics for AI and Deep Learning, we will review the exact Python syntax, data structures, and functions required to write PyTorch code effectively.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·