杰瑞科技汇

Python Anaconda在Mac上怎么安装配置?

Of course! Here is a comprehensive guide to using Python with Anaconda on a Mac, covering installation, setup, and common workflows.

Python Anaconda在Mac上怎么安装配置?-图1
(图片来源网络,侵删)

What is Anaconda and Why Use It on a Mac?

Anaconda is a free and open-source distribution of the Python and R programming languages for scientific computing (data science, machine learning, large-scale data processing, predictive analytics, etc.).

For Mac users, Anaconda is particularly popular because it simplifies a notoriously tricky part of Python development: managing packages and environments.

Key Benefits:

  • Solves Dependency Hell: It bundles Python with hundreds of the most popular data science packages (like NumPy, Pandas, Matplotlib, Jupyter, Scikit-learn, etc.), so you don't have to install them one by one.
  • Environment Management: This is Anaconda's superpower. It allows you to create isolated "virtual environments" for different projects. You can have Project A using Python 3.10 with version 1.5 of a library, and Project B using Python 3.11 with version 2.0, without them conflicting. This is crucial for reproducibility.
  • Cross-Platform: The same Anaconda setup works on macOS, Windows, and Linux, making it easy to share projects.
  • GUI Tools: It comes with graphical user interfaces like Anaconda Navigator (a simple point-and-click dashboard) and Anaconda Prompt (a command-line interface).
  • Large Package Repository: It uses its own channels (conda-forge, defaults) to provide pre-compiled packages, which is a huge advantage on macOS where compiling from source can be difficult.

Installation: Step-by-Step Guide

Step 1: Download the Anaconda Installer

  1. Go to the official Anaconda Distribution for macOS page.
  2. You will see two options:
    • Graphical Installer (Recommended for most users): A standard .pkg file that opens in Finder. It's very user-friendly.
    • Command-Line Installer: A .sh file for advanced users who prefer the terminal.

For this guide, we'll focus on the Graphical Installer.

Python Anaconda在Mac上怎么安装配置?-图2
(图片来源网络,侵删)

Step 2: Run the Installer

  1. Open your Downloads folder and double-click the Anaconda3-...-macOS-x86_64.pkg file.
  2. The installer will open. Click Continue.

Step 3: Follow the On-Screen Instructions

  1. Read and Agree to the License: You must agree to the terms to proceed.
  2. Choose Install Location: The default is recommended: "Install for me only". This installs Anaconda in your user directory (~/opt/anaconda3), which doesn't require administrator password and is generally safer. Click Continue.
  3. Installation Type: Just click Install. The installer will set up everything.
  4. Install Microsoft VS Code (Optional): The installer will ask if you want to install Microsoft's VS Code IDE. This is a great choice for beginners as it provides a powerful, free code editor. You can install it now or later.
  5. Installation Complete: Click Continue and then Close.

IMPORTANT: The installer will ask if you want to run conda init. Check the box and click "Install". This step modifies your shell configuration file (.zshrc for macOS Catalina and later, .bash_profile for older versions) so you can use the conda command directly from your terminal.


Post-Installation Setup and First Use

Step 1: Open a New Terminal

This is a critical step. The changes made by conda init only apply to new terminal sessions.

  • Close and reopen your Terminal application.
  • Alternatively, you can run source ~/.zshrc (or source ~/.bash_profile) in your current terminal to load the changes.

You will know it worked if you see (base) at the beginning of your command prompt.

(base) your-macname:~ your-username$

The (base) indicates that you are currently in the default "base" conda environment.

Python Anaconda在Mac上怎么安装配置?-图3
(图片来源网络,侵删)

Step 2: Verify the Installation

Let's make sure everything is working correctly. In your new terminal, type the following commands:

# Check the conda version
conda --version
# Check which Python conda is using
which python
# Expected output: /Users/your-username/opt/anaconda3/bin/python
# Check the Python version
python --version
# Expected output: Python 3.x.x

Step 3: Update Anaconda

It's good practice to update your Anaconda installation to the latest version.

# Update the conda package manager itself
conda update conda
# Update all packages in the current environment
conda update anaconda

Core Concepts: Environments and Packages

This is the most important part of using Anaconda effectively.

Environments

An environment is like a self-contained Python project folder. It has its own Python installation and its own set of packages.

Why create an environment?

  • Isolation: Keeps your project's dependencies separate from each other and from your system's Python.
  • Reproducibility: You can create a requirements.txt file (conda env export > environment.yml) that allows anyone (or your future self) to recreate the exact same environment.

Common conda commands for environments:

# Create a new environment named 'my_project' with Python 3.9
conda create --name my_project python=3.9
# Activate the environment (you must do this every time you open a new terminal)
conda activate my_project
# Your prompt will now change: (my_project) your-macname:~ your-username$
# Deactivate the current environment
conda deactivate
# The (my_project) prefix will disappear from your prompt
# List all environments
conda env list
# Delete an environment
conda env remove --name my_project

Packages

Once your environment is activated, you can install packages. The conda command is smart because it finds compatible versions of all required dependencies.

# (Make sure your environment is activated first!)
conda activate my_project
# Install a single package
conda install pandas
# Install multiple packages at once
conda install numpy matplotlib scikit-learn
# Install a specific version of a package
conda install python=3.9.7
# Remove a package
conda remove pandas
# List all packages in the current environment
conda list

Common Workflows

Workflow 1: The Data Scientist's Workflow

  1. Start Terminal.
  2. Create and activate a new environment for a new project.
    conda create --name data_analysis python=3.10
    conda activate data_analysis
  3. Install necessary libraries.
    conda install pandas numpy matplotlib seaborn jupyter
  4. Launch Jupyter Notebook.
    jupyter notebook

    This will open a new tab in your default web browser where you can create and run notebooks.

  5. When you're done, close the notebook and deactivate the environment.
    # In the terminal, press Ctrl+C to stop the Jupyter server
    conda deactivate

Workflow 2: The Developer's Workflow (VS Code)

  1. Install VS Code (if you didn't during the Anaconda installer).
  2. Install the Python Extension for VS Code from the marketplace.
  3. Open your project folder in VS Code (File > Open Folder...).
  4. Open the integrated Terminal in VS Code (View > Terminal or Ctrl+\).
  5. Create and activate a conda environment from the terminal (as shown in Workflow 1).
  6. Select the Python Interpreter. VS Code will detect your active conda environment. Click on the Python version in the bottom-right corner of the status bar and select the interpreter from your activated environment (e.g., Python 3.10 ('data_analysis': conda)).
  7. Now you can create .py files, and VS Code will use the correct Python environment and libraries.

Troubleshooting Common Issues

  • Problem: 'conda' command not found.

    • Solution: You likely forgot to close and reopen your Terminal after installation. If that doesn't work, run source ~/.zshrc (or source ~/.bash_profile).
  • Problem: I installed a package but my script can't import it.

    • Solution: You are probably in the wrong environment. Check your terminal prompt for (base) or your environment name. If it says (base), you need to activate your project's environment with conda activate my_project.
  • Problem: A package installation is failing or taking a very long time.

    • Solution: This is common. Try using the conda-forge channel, which has a wider selection of up-to-date packages.
      # Install from conda-forge
      conda install -c conda-forge package_name

      You can also set conda-forge as your default channel to avoid typing -c conda-forge every time:

      conda config --add channels conda-forge
      conda config --set channel_priority strict
  • Problem: I want to use the system Python, not Anaconda's.

    • Solution: That's fine! The best practice is to keep the base environment clean and untouched. Always create new environments for your projects (conda create --name my_project). This way, the system Python remains unaffected, and your Anaconda installation is self-contained.
分享:
扫描分享到社交APP
上一篇
下一篇