Chapter 0: Environment Setup and Development Tools (macOS, Standard Python)
Table of contents (10)
Language / 语言: English | 中文版
We use two software packages in this course:
- Python (Official Distribution): Provides the Python interpreter and the pip package manager.
- 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:
- Click the Apple menu () in the top-left corner of the screen.
- Select About This Mac.
- 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
- Open your browser and visit the official Python macOS download page.
- Locate the latest stable release (Latest Python 3 Release).
- Find the macOS installer, usually named macOS 64-bit universal2 installer (with a filename like
python-3.12.x-macos11.pkg). - The Universal2 format supports both Apple silicon and Intel processors.
- Click the link to download the
.pkginstaller.
0.1.3 Installation Steps
Open Finder, go to Downloads, double-click the .pkg installer, and follow the wizard:
- Introduction and License Agreement:
- Click Continue.
- Read the license agreement, click Continue, and then click Agree.
- Destination and Installation:
- The installer places Python into
/Library/Frameworks/Python.frameworkand creates symbolic links in/usr/local/bin. - Click Install.
- The installer places Python into
- Authorize Installation:
- Enter your Mac login password or touch Touch ID to authorize the installation.
- 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:
- Open Finder and click Applications in the left sidebar.
- Locate the folder named Python 3.12 (the version number matches your installation) and double-click it.
- Locate the script file named Install Certificates.command.
- Double-click the script. The system opens a terminal window and downloads the root certificates.
- 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.
- Press
Cmd + Spaceto open Spotlight Search. - Type
Terminaland press Enter to open the terminal window. - At the prompt, enter the following command and press Enter:If the output displays a version string likebash
python3 --versionPython 3.12.x, the Python interpreter is ready. - Enter the next command and press Enter:If the output displays a string likebash
pip3 --versionpip 24.x.x from ... (python 3.12), pip is functioning properly. - Close the terminal window.
0.2 Install Visual Studio Code
0.2.1 Download the Installer
- Open your browser and visit the VS Code download page.
- 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.
- The browser downloads the
.dmgdisk image file (or a.ziparchive).
0.2.2 Move VS Code to the Applications Folder
Example: Install VS Code and launch the editor from the Applications folder.
- Double-click the downloaded
.dmgfile in Downloads. - In the disk image window, drag the Visual Studio Code.app icon into the Applications folder icon.
- When the copy finishes, eject the disk image. If you downloaded a
.zipfile instead, decompress it and drag the extracted application into Applications.
0.2.3 First Launch
- Open Finder, go to the Applications folder, and double-click Visual Studio Code.
- 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):
- Open VS Code and click the four-square icon in the left activity bar (Extensions, shortcut:
Cmd + Shift + X). - Type
Chinese(or your preferred language) in the search box. - Find the official language pack published by Microsoft (such as Chinese (Simplified) Language Pack for Visual Studio Code).
- Click Install.
- 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
- Click the Extensions icon in the left activity bar (or press
Cmd + Shift + X). - In the search box, enter
Python. - Locate the official Python extension published by Microsoft (marked with the blue and yellow Python logo).
- 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:
- Press
Cmd + Shift + P(or choose "View" -> "Command Palette..." from the top menu) to open the command palette. - Type
Python: Select Interpreterand select the matching command. - VS Code displays the detected Python environments. Select the official Python 3 interpreter (typically located at
/usr/local/bin/python3or/Library/Frameworks/Python.framework/Versions/3.12/bin/python3). - 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_codefolder in Documents and open it in VS Code.
- Open Finder and click Documents in the left sidebar.
- Right-click an empty area (or tap with two fingers on the trackpad), select "New Folder", and name it
python_code. - In VS Code, select File -> Open Folder... from the top menu (shortcut:
Cmd + O). - Select the
python_codefolder and click Open. - 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
- In the VS Code Explorer sidebar on the left, locate the
PYTHON_CODEfolder header. - Hover over the folder name and click the New File icon (a file icon with a plus sign).
- Type the filename
hello.pyand press Enter. (Python source files use the.pyextension.) - An empty editor tab for
hello.pyopens.
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.
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:
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
- Save
hello.pyin VS Code. - Trigger "Run Python File in Terminal".
- The selected Python interpreter executes the file.
- 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
pythonin the terminal returnszsh: command not found: python. - Cause: macOS removed earlier legacy installations of Python 2. Official Python 3 installers register the command as
python3to avoid naming conflicts. - Solution:
- Use
python3andpip3in the terminal for routine work. - If you prefer typing
python, add a shell alias by running these commands in the terminal:Then runbashecho 'alias python="python3"' >> ~/.zshrc source ~/.zshrcpython --versionto check the result.
- Use
0.6.2 pip Reports SSL Certificate Verification Error
- Symptom: Running
pip3 installfails with anSSL: CERTIFICATE_VERIFY_FAILEDerror. - Cause: The certificate installation script has not been run.
- Solution: Open Finder -> Applications -> open the
Python 3.12folder -> 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:
- Click the Apple menu () -> System Settings.
- Select Privacy & Security in the sidebar.
- Scroll down to the "Security" section to locate the blocked application notice.
- 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:
- Confirm that the filename ends in
.py. If saved as text (such as.txt), VS Code will not activate Python tooling. - Press
Cmd + Shift + P, typePython: Select Interpreter, and press Enter. - In the list that appears, select the path containing
Python 3.12.
- Confirm that the filename ends in
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:
- Click File in the top menu bar.
- Select Auto Save.
- 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.
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 (typeexit()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.