Skip to content

Chapter 0: Environment Setup and Development Tools (macOS, Standard Python)

Updated: 2026-09-08 19:10 · English
Table of contents (10)

Language / 语言: English | 中文版

We use two software packages in this course:

  1. Python (Official Distribution): Provides the Python interpreter and the pip package manager.
  2. Visual Studio Code (VS Code): A code editor used to write and edit programs.

Version Note (2026-09-08): Current official stable Python installers for macOS use the Universal2 format, natively supporting both Apple silicon (M-series) and Intel Macs, requiring macOS 11.0 (Big Sur) or newer. Check the official system requirements and release notes before downloading.

Version numbers shown here illustrate output formats; actual numbers may vary. Software menus and button labels may also change over time. Windows users should read the Windows version (Standard Python). If you need an environment configured for data analysis and scientific computing, read the macOS version (Anaconda).

This chapter covers downloading, installing, and configuring both tools on macOS, followed by writing and running your first Python program. After running the program successfully, we examine how these tools work together.


Part 1: Core Track

0.1 Install Official Python

0.1.1 Identify Chip Type and System Version

Apple computers use two processor architectures. Check your computer hardware and macOS version before downloading:

  1. Click the Apple menu () in the top-left corner of the screen.
  2. Select About This Mac.
  3. Record the macOS version, then check the "Chip" or "Processor" line:
    • If the entry displays an Apple M-series processor (such as M1, M2, M3, or M4), your computer uses Apple silicon.
    • If the entry displays an Intel Core i5 / i7, your computer uses an Intel chip.

Once you know your system information, proceed with the download.

0.1.2 Download the Installer

  1. Open your browser and visit the official Python macOS download page.
  2. Locate the latest stable release (Latest Python 3 Release).
  3. Find the macOS installer, usually named macOS 64-bit universal2 installer (with a filename like python-3.12.x-macos11.pkg).
  4. The Universal2 format supports both Apple silicon and Intel processors.
  5. Click the link to download the .pkg installer.

0.1.3 Installation Steps

Open Finder, go to Downloads, double-click the .pkg installer, and follow the wizard:

  1. Introduction and License Agreement:
    • Click Continue.
    • Read the license agreement, click Continue, and then click Agree.
  2. Destination and Installation:
    • The installer places Python into /Library/Frameworks/Python.framework and creates symbolic links in /usr/local/bin.
    • Click Install.
  3. Authorize Installation:
    • Enter your Mac login password or touch Touch ID to authorize the installation.
  4. Finish Setup:
    • When the progress bar finishes and reports success, click Close.
    • Click Move to Trash when prompted to delete the installer file.

0.1.4 Install SSL Certificates

Official Python releases on macOS use an isolated certificate directory. Run the certificate installation script provided with Python to enable secure network downloads:

  1. Open Finder and click Applications in the left sidebar.
  2. Locate the folder named Python 3.12 (the version number matches your installation) and double-click it.
  3. Locate the script file named Install Certificates.command.
  4. Double-click the script. The system opens a terminal window and downloads the root certificates.
  5. When the terminal prints [Process completed], certificate setup is complete. Close the terminal window.

0.1.5 Verify the Installation

After installation, verify that Python and pip run correctly:

Example: Inspect the installed Python 3 and pip 3 version numbers in the macOS terminal.

  1. Press Cmd + Space to open Spotlight Search.
  2. Type Terminal and press Enter to open the terminal window.
  3. At the prompt, enter the following command and press Enter:
    bash
    python3 --version
    
    If the output displays a version string like Python 3.12.x, the Python interpreter is ready.
  4. Enter the next command and press Enter:
    bash
    pip3 --version
    
    If the output displays a string like pip 24.x.x from ... (python 3.12), pip is functioning properly.
  5. Close the terminal window.

0.2 Install Visual Studio Code

0.2.1 Download the Installer

  1. Open your browser and visit the VS Code download page.
  2. Under the macOS logo, select the download for your processor:
    • Select Apple silicon for Apple chips.
    • Select Intel chip for Intel processors.
    • Or select Universal to support both.
  3. The browser downloads the .dmg disk image file (or a .zip archive).

0.2.2 Move VS Code to the Applications Folder

Example: Install VS Code and launch the editor from the Applications folder.

  1. Double-click the downloaded .dmg file in Downloads.
  2. In the disk image window, drag the Visual Studio Code.app icon into the Applications folder icon.
  3. When the copy finishes, eject the disk image. If you downloaded a .zip file instead, decompress it and drag the extracted application into Applications.

0.2.3 First Launch

  1. Open Finder, go to the Applications folder, and double-click Visual Studio Code.
  2. On first launch, macOS displays a security prompt stating that Visual Studio Code was downloaded from the internet. Click Open to proceed to the main window.

0.3 Initial VS Code Setup

A fresh VS Code installation uses English by default and needs the Python extension. Complete these three configuration steps:

0.3.1 Install Language Pack (Optional)

If you prefer an interface language other than English (such as Simplified Chinese):

  1. Open VS Code and click the four-square icon in the left activity bar (Extensions, shortcut: Cmd + Shift + X).
  2. Type Chinese (or your preferred language) in the search box.
  3. Find the official language pack published by Microsoft (such as Chinese (Simplified) Language Pack for Visual Studio Code).
  4. Click Install.
  5. When prompted in the lower-right corner to "Change Language and Restart", click the button to restart VS Code with the new language.

0.3.2 Install the Python Extension

  1. Click the Extensions icon in the left activity bar (or press Cmd + Shift + X).
  2. In the search box, enter Python.
  3. Locate the official Python extension published by Microsoft (marked with the blue and yellow Python logo).
  4. Click Install and wait for the installation to finish.

0.3.3 Select the Official Python Interpreter

Connect VS Code to the installed Python environment:

  1. Press Cmd + Shift + P (or choose "View" -> "Command Palette..." from the top menu) to open the command palette.
  2. Type Python: Select Interpreter and select the matching command.
  3. VS Code displays the detected Python environments. Select the official Python 3 interpreter (typically located at /usr/local/bin/python3 or /Library/Frameworks/Python.framework/Versions/3.12/bin/python3).
  4. After selection, the bottom status bar displays the active environment name, such as Python 3.12.x.

0.4 Write and Run Your First Program

Next, create a dedicated project folder, then save and run your first program.

0.4.1 Create a Working Directory

Keep your course programs in a single folder for organized access.

Example: Create a python_code folder in Documents and open it in VS Code.

  1. Open Finder and click Documents in the left sidebar.
  2. Right-click an empty area (or tap with two fingers on the trackpad), select "New Folder", and name it python_code.
  3. In VS Code, select File -> Open Folder... from the top menu (shortcut: Cmd + O).
  4. Select the python_code folder and click Open.
  5. When prompted with the dialog asking "Do you trust the authors of the files in this folder?", click Yes, I trust the authors.

0.4.2 Create a Source File

  1. In the VS Code Explorer sidebar on the left, locate the PYTHON_CODE folder header.
  2. Hover over the folder name and click the New File icon (a file icon with a plus sign).
  3. Type the filename hello.py and press Enter. (Python source files use the .py extension.)
  4. An empty editor tab for hello.py opens.

0.4.3 Write and Execute the Code

In the hello.py editor tab, type this line:

Example: Run one line of code to print Hello, world! to the terminal.

python
print("Hello, world!")

Press Cmd + S to save the file. (An unsaved file shows a solid dot next to its filename; saving removes the dot.)

Running the Program (Choose Either Method):

  • Method 1 (Recommended): Click the triangular Play button in the upper-right corner of the editor (tooltip: "Run Python File in Dedicated Terminal").
  • Method 2: Right-click anywhere in the code editor and choose "Run Python File in Terminal".

VS Code opens an integrated terminal panel at the bottom and prints the output:

text
Hello, world!

Seeing this text confirms that the Python interpreter executed hello.py successfully. Replace the text inside the quotes with your name, run the file again, and observe the new output.


0.5 Understanding the Tools

0.5.1 The Python Interpreter and pip

The Python interpreter reads and executes Python code. When you ran hello.py, the interpreter executed the print(...) statement and wrote text to the terminal.

The standard Python distribution provides the official CPython interpreter. On macOS, the system command is named python3, and the package manager is named pip3. The environment contains the core language and standard library. When external libraries are required later, install them with pip3.

0.5.2 VS Code

VS Code is a code editor. It highlights syntax, suggests autocompletions, and hosts an integrated terminal in one window. With the Python extension installed, it detects interpreters and executes .py files.

0.5.3 From Source File to Program Output

  1. Save hello.py in VS Code.
  2. Trigger "Run Python File in Terminal".
  3. The selected Python interpreter executes the file.
  4. The terminal displays the printed results.

Writing code and running code are separate steps. Knowing which file and which interpreter you are running makes it easier to diagnose issues, such as code changes that do not seem to take effect or packages that cannot be found.


Part 2: Supplementary Notes

0.6 Troubleshooting Common Issues (FAQ)

0.6.1 Terminal Message: zsh: command not found: python

  • Symptom: Entering python in the terminal returns zsh: command not found: python.
  • Cause: macOS removed earlier legacy installations of Python 2. Official Python 3 installers register the command as python3 to avoid naming conflicts.
  • Solution:
    • Use python3 and pip3 in the terminal for routine work.
    • If you prefer typing python, add a shell alias by running these commands in the terminal:
      bash
      echo 'alias python="python3"' >> ~/.zshrc
      source ~/.zshrc
      
      Then run python --version to check the result.

0.6.2 pip Reports SSL Certificate Verification Error

  • Symptom: Running pip3 install fails with an SSL: CERTIFICATE_VERIFY_FAILED error.
  • Cause: The certificate installation script has not been run.
  • Solution: Open Finder -> Applications -> open the Python 3.12 folder -> double-click Install Certificates.command. Wait until the terminal reports [Process completed].

0.6.3 macOS Security Alert: Cannot Be Opened Because Developer Cannot Be Verified

  • Symptom: macOS displays a security prompt blocking an installer or decompressed application.
  • Solution:
    1. Click the Apple menu () -> System Settings.
    2. Select Privacy & Security in the sidebar.
    3. Scroll down to the "Security" section to locate the blocked application notice.
    4. Click Open Anyway and enter your administrator password to authorize.

0.6.4 Missing Run Button or Unselected Interpreter

  • Symptom: The triangular run button does not appear in the top-right corner, or a prompt says Select an Interpreter.
  • Solution:
    1. Confirm that the filename ends in .py. If saved as text (such as .txt), VS Code will not activate Python tooling.
    2. Press Cmd + Shift + P, type Python: Select Interpreter, and press Enter.
    3. In the list that appears, select the path containing Python 3.12.

0.7 Productivity Settings

0.7.1 Enabling Auto Save

Saving files with Cmd + S before running them is a good habit. The "Run Python File in Terminal" command in the Python extension also saves the active file before execution, though other launch methods may not. See the official run documentation.

To enable Auto Save in VS Code:

  1. Click File in the top menu bar.
  2. Select Auto Save.
  3. VS Code saves dirty files automatically according to its configuration.

0.7.2 macOS Keyboard Habits

In macOS, keyboard shortcuts differ slightly from Windows:

  • Many shortcuts use the Command key (⌘) on macOS where Windows uses the Ctrl key.
  • The Option key (⌥) is often labeled Alt.

Common VS Code shortcuts on macOS:

Shortcut Description
Cmd + S Save the active file
Cmd + / Toggle single-line comment (adds or removes #)
Cmd + Shift + P Open the Command Palette
Cmd + B Show or hide the primary sidebar
Ctrl + Backtick Show or hide the integrated terminal (uses the Control key)

0.8 Basic pip and Python Commands

Most course programs run directly from VS Code. If you want to use the command line, run these commands in Terminal:

Example: Inspect the list of installed Python packages in the macOS terminal.

bash
pip3 list

Common Commands:

  • python3 --version: Print the active Python 3 version.
  • pip3 --version: Print the active pip 3 version.
  • pip3 list: List all installed third-party packages.
  • pip3 install package_name: Download and install a package from PyPI.
  • pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple package_name: Download using a mirror repository.
  • python3: Start the interactive Python shell (type exit() to quit).
  • python3 filename.py: Execute a script file directly.

Self-Check

Record your macOS version, chip type, downloaded installer filename, and the output of python3 --version. Check the official documentation to confirm that the installer matches your system architecture. Then modify the printed text in hello.py and run it again.


Navigation: Course overview (Chinese) | Next: Chapter 1 Basic Data Types and Operations (Chinese)

The English version of Chapter 1 is not yet available.