Chapter 0: Environment Setup and Development Tools (macOS, Anaconda)
Table of contents (10)
Language / 语言: English | 中文版
We use two software packages in this course:
- Anaconda: Provides the Python runtime environment and the Conda package manager.
- Visual Studio Code (VS Code): A code editor used to write and edit programs.
Version Note (2026-09-08): Current macOS installers for Anaconda Distribution target Apple silicon, requiring macOS 12.1 or newer. Intel Mac users can select older releases from the official archive; the 2025.06 release series is the final distribution supporting Intel Macs. Check the system requirements and release notes before installing.
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 (Anaconda). If you prefer a lightweight, standard Python setup, read the macOS version (Standard Python).
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 Anaconda
0.1.1 Identify Chip Type and System Version
Apple computers use two processor architectures. Check your computer hardware 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 "Chip" lists an Apple M-series processor, your computer uses Apple silicon.
- If "Processor" lists an Intel Core i5 / i7, your computer uses an Intel chip.
Once you know your processor type, proceed with the matching download.
0.1.2 Download the Installer
- For Apple silicon, visit the Anaconda download page and select the Apple silicon Graphical Installer. The filename contains
MacOSX-arm64.pkg. - For an Intel Mac, visit the official archive and locate an installer containing
MacOSX-x86_64.pkg. Choose a version compatible with your macOS release by consulting the legacy OS installation notes and release notes. - If prompted to register, click Skip registration to view the download links.
- Record the installer filename. Note the distinction between
.pkggraphical installers and.shshell scripts; use the.pkginstaller here.
0.1.3 Installation Steps
Open Finder, locate the .pkg file in Downloads, double-click it, and follow the installation wizard:
- Permission Prompt: If the system displays a dialog stating that the package will run a program to determine if software can be installed, click Allow or Continue.
- Introduction: Click Continue.
- License Agreement: Read the agreement, click Continue, and then click Agree.
- Select Installation Type:
- Review the installation destination offered by the installer. Note the exact path and click Continue. This path will be needed when configuring the interpreter or diagnosing environment issues.
- Start Installation:
- Click Install.
- Enter your Mac login password or touch Touch ID to authorize the installation.
- Wait for Completion: Installation time depends on your computer hardware. When the installer reports success, click Close.
- Clean Up: When asked whether to move the installer to the Trash, click Move to Trash to free disk space.
0.1.4 Verify the Installation
After installation, verify that Conda and Python run correctly:
Example: Inspect the installed Conda and Python version numbers using 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
conda --versionconda 24.x.x, Conda is ready. - Enter the next command and press Enter:If the output displays a version string likebash
python --versionPython 3.12.x, Python is installed correctly. You can now close the terminal window.
0.2 Install Visual Studio Code
0.2.1 Download the Installer
- Visit the VS Code download page and check the macOS requirements.
- Choose Apple silicon or Intel chip based on your computer hardware, or choose Universal to support both.
- Download the
.dmgdisk image file.
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
.dmgfile in your Downloads folder. - In the disk image window, drag Visual Studio Code.app into the Applications folder.
- Open VS Code from the Applications folder.
These steps apply to current .dmg installers. If you downloaded a .zip archive, decompress it first, then drag the application to Applications. See the VS Code macOS setup guide.
0.2.3 First Launch
- Open Finder, go to the Applications folder, and double-click Visual Studio Code (or open it from Launchpad).
- On first launch, macOS displays a confirmation dialog 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 Anaconda Python Interpreter
Connect VS Code to the newly installed Anaconda 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 entry labeled with
condaorbase(typically pointing to/opt/anaconda3/bin/pythonor~/anaconda3/bin/python). - After selection, the bottom status bar displays the active environment name, such as
Python 3.12.x ('base': conda).
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 hold Control and click), 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 Anaconda
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.
Anaconda provides Python together with a collection of standard packages. This chapter uses the Python environment included in Anaconda. Conda manages packages and virtual environments, allowing you to install additional libraries when needed.
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: conda
This message indicates that the shell cannot locate the conda command. Review the installation directory recorded during setup to identify where Anaconda is installed.
Example: Activate an Anaconda installation at
/opt/anaconda3and initialize Conda for future zsh sessions.
source /opt/anaconda3/bin/activate
conda init zsh
The path /opt/anaconda3 is an example. If Anaconda is installed in ~/anaconda3 or another directory, replace the first line with the actual path to bin/activate. Close and reopen the terminal, then run conda --version to verify. If errors persist, consult the Anaconda troubleshooting guide.
0.6.2 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 Python path containing
conda.
- 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 Conda Commands
Most course programs run directly from VS Code. If you want to inspect your environment in the terminal, open Terminal and run these commands:
Example: List all packages installed in the active Anaconda environment.
conda list
Common Commands:
conda --version: Print the Conda version.conda list: List installed packages and versions.python: Start the interactive Python shell (typeexit()to quit).python filename.py: Execute a script file directly.
Self-Check
Record your macOS version, chip type, Anaconda installer filename, and the output of python --version. Review the official documentation to explain why this installer matches your computer 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.