Setting Up Python and PyTorch Environment
# 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
pipwith 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.
Go to
python.org/downloadsand download the Windows installer.
- 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.
Open terminal and install Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
-
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. Open your terminal/command prompt.
-
2.
Create a folder for this course:
mkdir pytorchcourseand enter it:cd pytorchcourse
-
3.
Create the virtual environment (named
ptenv):
-
Windows:
python -m venv ptenv
-
Mac/Linux:
python3 -m venv ptenv
- 4. Activate the environment:
-
Windows:
ptenv\Scripts\activate
-
Mac/Linux:
source ptenv/bin/activate
(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:
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.
Download Visual Studio Code from
code.visualstudio.com.
- 2. Open VS Code, go to the Extensions tab (squares on the left menu).
- 3. Search for and install the Python extension and the Jupyter extension.
-
4.
Open your
pytorchcoursefolder in VS Code.
-
5.
Create a new file named
hellopytorch.ipynb.
-
6.
Open the file, click "Select Kernel" in the top right, and choose your
pt_envenvironment.
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):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.
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 runptenv\Scripts\activatefirst.
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. Follow the matrix on the PyTorch website and install PyTorch into your virtual environment.
- 2. Run the verification script to check your version and GPU status.
12. MCQ Quiz with Answers
Why must you use a Virtual Environment when installing PyTorch?
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?