Skip to content

Chapter 2: Environment Setup

Installing Python on Windows

  1. Visit python.org/downloads and click the yellow "Download Python 3.x.x" button
  2. Run the installer — make sure to check ✅ Add Python to PATH (this step is crucial!)
  3. Click "Install Now" and wait for installation to complete
  4. Press Win+R, type cmd to open Command Prompt, and enter:
bash
python --version
bash
Python 3.12.4

⚠️ Note: ⚠️ If you forgot to check "Add Python to PATH", the system will report it can't find the python command. Solution: re-run the installer, select "Modify", and check the PATH option.

Installing Python on macOS

macOS comes with Python pre-installed, but it's usually an older version. It's recommended to install via Homebrew:

bash
# Install Homebrew (if you haven't already)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Install Python 3
brew install python

Installing Python on Linux

Most Linux distributions come with Python 3 pre-installed. If you need the latest version:

bash
# Ubuntu/Debian
sudo apt update && sudo apt install python3 python3-pip python3-venv

# CentOS/RHEL
sudo yum install python3

# Arch Linux
sudo pacman -S python python-pip

pip Configuration

pip is Python's package manager — the equivalent of an app store on your phone. Configuring a mirror source can significantly boost download speeds:

bash
# Set Tsinghua mirror as default download source
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
bash
Writing to /home/user/.config/pip/pip.conf

Virtual Environments (venv)

Imagine you're cooking in a kitchen. Different dishes need different seasonings — if you mix them all together, the flavors get muddled. A virtual environment gives each project its own "independent kitchen."

bash
# Create virtual environment
python3 -m venv myproject_env

# Activate virtual environment (Linux/macOS)
source myproject_env/bin/activate

# Activate virtual environment (Windows)
myproject_env\Scripts\activate

# After activation, the terminal prompt will change
(myproject_env) $ pip install requests

# Deactivate virtual environment
deactivate

💡 Tip: 💡 Good habit: Create a dedicated virtual environment for every Python project, and record dependencies in a requirements.txt file.