在 macOS 上进行开发和机器学习是一个高效的选择,得益于其 Unix 基础、出色的硬件支持以及丰富的工具链。以下是详细的指南:
git、autojump)。bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"brew install git。brew install gh。pyenv 管理多版本:
bash
brew install pyenv
pyenv install 3.9.7 # 安装指定版本
pyenv global 3.9.7 # 设为默认版本python -m venv myenv 或 conda(见下文)。nvm 管理:
bash
brew install nvm
nvm install 16Miniforge/Conda:推荐使用 Conda 的社区版本(兼容 M1/M2):
# 安装 Miniforge(ARM 版)
curl -L https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-arm64.sh -o Miniforge3.sh
bash Miniforge3.sh
创建环境:
conda create -n ml python=3.9
conda activate ml
关键库安装:
pip install numpy pandas matplotlib scikit-learn jupyter
conda install -c apple tensorflow-deps
pip install tensorflow-macos tensorflow-metal # GPU 加速
pip install torch torchvision torchaudio
python
import torch
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")jupyter lab 启动,推荐扩展:
bash
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --userbrew install postgresqlbrew install redisbrew install htoppip install nvitop/usr/bin/python3),始终用 pyenv 或 Conda。sudo pip,优先使用 --user 或虚拟环境。opencv 需通过 brew install opencv 安装)。通过以上配置,你的 macOS 将成为一个强大的开发和机器学习平台。如有具体问题(如特定库的安装或性能优化),欢迎进一步提问!