跳转至

Of course. You are right, integrating updates can make a guide confusing. Here is a clean, unified, and concise guide from start to finish.

This is the complete, modern workflow for setting up a professional Python development environment on an Apple Silicon Mac.

The Philosophy

  1. Isolate Everything: Never use the system Python. Every project gets its own Python version and its own libraries.
  2. Automate Quality: Use tools to format and check your code automatically.
  3. Control Every Change: Use Git for version control from the very first line of code.

Part 1: One-Time System Setup

You only need to do this part once.

Step 1: Install Developer Tools Install Apple's essential compilers and Git. Open the Terminal and run:

xcode-select --install

Step 2: Install Homebrew Get the essential package manager for macOS:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
After it finishes, run the two commands it tells you to. This adds Homebrew to your PATH.

Step 3: Install and Configure pyenv Install the best tool for managing multiple Python versions:

brew install pyenv
Now, configure your shell to use it. (This assumes you use the default Zsh shell):
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
Important: Close and reopen your Terminal for these changes to take effect.

Step 4: Install Python Use pyenv to install a recent, clean version of Python and set it as your default:

# Install Python (this will take a few minutes)
pyenv install 3.12.0

# Set this version as your global default
pyenv global 3.12.0

Step 5: Configure Git Tell Git who you are. This name and email will be attached to all your commits.

git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
git config --global init.defaultBranch main

Step 6: Install VS Code and Extensions 1. Download and install Visual Studio Code from the official website. 2. Open VS Code. Press Cmd + Shift + P, type Shell Command, and select "Shell Command: Install 'code' command in PATH". 3. Go to the Extensions tab (the icon with four squares) and install these two extensions: * Python (from Microsoft) * Ruff (from Astral Software)

Your system is now fully prepared.


Part 2: The Per-Project Workflow

Follow these steps every time you start a new project.

Step 1: Create Project & Initialize Git

# Create a folder and move into it
mkdir my-project
cd my-project

# Initialize it as a Git repository
git init

Step 2: Create a .gitignore File This is the most important step for clean version control. Create a file named .gitignore and add the following content to it. This tells Git to ignore temporary files and secrets.

# Python
__pycache__/
*.pyc
.venv
.env

# macOS / Editor
.DS_Store
.vscode/

Step 3: Create a Virtual Environment Isolate your project's dependencies in a local .venv folder.

# The command below uses your pyenv-managed Python
python -m venv .venv

Step 4: Activate Environment & Install Packages Activate the environment to start using it.

source .venv/bin/activate
Your terminal prompt will now start with (.venv). Now, you can install libraries, and they will only be installed for this specific project.
pip install pandas numpy ruff

Step 5: Make Your First Commit Save the initial state of your project.```bash

Save a list of your dependencies

pip freeze > requirements.txt

Add all project files to Git

git add .

Save a snapshot of your project with a message

git commit -m "Initial commit: Set up project structure and dependencies"

**Step 6: Start Coding**
Open the entire project folder in VS Code.
```bash
code .
VS Code will automatically detect your .venv virtual environment. Now you can create your Python files (e.g., main.py).

To automate code formatting, create a .vscode folder with a settings.json file inside it and add:

{
  "[python]": {
    "editor.defaultFormatter": "charliermarsh.ruff",
    "editor.formatOnSave": true
  }
}
Now, every time you save a Python file, Ruff will automatically format it for you. This workflow ensures every project is isolated, reproducible, and uses professional code quality standards from the start.