Chapter 2: Environment Setup
Installing Python on Windows
- Visit python.org/downloads and click the yellow "Download Python 3.x.x" button
- Run the installer — make sure to check ✅ Add Python to PATH (this step is crucial!)
- Click "Install Now" and wait for installation to complete
- Press Win+R, type
cmdto open Command Prompt, and enter:
python --versionPython 3.12.4⚠️ Note: ⚠️ If you forgot to check "Add Python to PATH", the system will report it can't find the
pythoncommand. 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:
# 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 pythonInstalling Python on Linux
Most Linux distributions come with Python 3 pre-installed. If you need the latest version:
# 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-pippip 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:
# Set Tsinghua mirror as default download source
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simpleWriting to /home/user/.config/pip/pip.confVirtual 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."
# 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.txtfile.