macOS 安装 python

本文档介绍了如何在macOS Sierra 10.12.1上使用HomeBrew安装Python,建议避免使用系统自带的Python,通过HomeBrew安装最新版本,并通过`brew switch`命令切换到正确版本。同时提到了Pip的升级和使用pip2或pip3确保与对应Python版本匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境:  macOS Sierra 10.12.1

不要用mac系统自带的python, 用brew重新装一个,然后把/usr/local/bin/python指向brew目录下的python.

摘自: http://stackoverflow.com/questions/17271319/how-to-install-pip-on-mac-os-x

Installing a separate copy of Python is a popular option, even though Python already comes with MacOS. You take on the responsibility to make sure you're using the copy of Python you intend. But, the benefits are having the latest Python release and some protection from hosing your system if things go badly wrong.

To install Python using HomeBrew:
```sh
brew update
brew install python # or brew install python3
```
Now confirm that we're working with our newly installed Python:
```sh
ls -lh `which python`
```
...should show a symbolic link to a path with "Cellar" in it like:
```sh
lrwxr-xr-x  1 chris  admin    35B Dec  2 13:40 /usr/local/bin/python -> ../Cellar/python/2.7.8_2/bin/python
```
**cyper notes: **我没有指到这个目录, 我用`brew switch python x`命令切了一下, 就正常了, 结果如下:
```sh
?  ~ brew switch python x
Error: python does not have a version "x" in the Cellar.
Versions available: 2.7.12_2
?  ~ brew switch python 2.7.12_2
Cleaning /usr/local/Cellar/python/2.7.12_2
39 links created for /usr/local/Cellar/python/2.7.12_2
?  ~ ls -lh `which python`
lrwxr-xr-x  1 cyper  admin    36B Dec  5 16:31 /usr/local/bin/python -> ../Cellar/python/2.7.12_2/bin/python
?  ~

```

Pip should be installed along with Python. You might want to upgrade it by typing:
```
pip install --upgrade pip
```
Now you're ready to install any of the 50,000+ packages on PyPI.

### Other Notes

Formerly, I've used get-pip.py to install pip. But, the docs warn that get-pip.py does not coordinate with package managers and may leave your system in an inconsistent state. Anyway, there's no need, given that pip is now included with Python as of 2.7.9.

Note that pip isn't the only package manager for Python. There's also easy_install. It's no good to mix the two, so don't do it.

Finally, if you have both Python 2 and 3 installed, pip will point to whichever Python you installed last. Get in the habit of explicitly using either pip2 or pip3, so you're sure which Python is getting the new library.

Happy hacking!
<think>首先,用户的问题是:“我想在MacOS安装Python的指导 请问如何在MacOS安装Python”。这是一个明确的请求,需要提供在MacOS安装Python的步骤。 根据系统级指令: - 所有行内数学表达式必须使用$...$格式,但在这个上下文中,安装Python不涉及数学表达式,所以我不需要添加任何数学内容。 - 独立公式必须使用$$...$$格式,但同样,这里不适用。 - LaTeX语法正确:确保任何LaTeX使用正确。 - 使用中文回答:整个响应必须用中文。 - 生成相关问题:在回答结束时,添加相关问题。 - 回答中引用的段落末尾自然地添加引用标识:由于用户提供了引用,我需要参考它们。引用包括[^1], [^2], [^3], [^4],这些是关于在MacOS安装Python的指南。 用户提供的引用: - [^1]: Python入门指南:如何在MacOS安装Python,提到检查MacOS是否预装Python。 - [^2]: 另一个Python安装指南。 - [^3]: macOS上搭建Python环境,显示了一个终端输出示例。 - [^4]: MAC OS安装Python教程,提到安装Homebrew。 用户说:“上述引用仅供参考,并不是用户真正的需求”,但我应该参考用户上一次的问题和我的回答。用户的问题是新的,所以我需要基于这些引用构建一个可靠的响应。 回答结构: - 结构清晰,逐步解决问题。 - 保证回答真实可靠:提供准确的信息。 步骤概述: 1. **检查预装Python**:MacOS通常预装Python 2.7,但用户可能需要Python 3。 2. **安装方法**: - 使用官方Python安装器。 - 使用Homebrew。 - 使用Anaconda(可选)。 3. **验证安装**:在终端检查Python版本。 参考引用: - 从[^1],提到检查预装Python。 - 从[^2],指导安装和更新到最新版本。 - 从[^3],显示终端输出。 - 从[^4],提到安装Homebrew。 完整的步骤: 1. 检查系统是否已安装Python。 2. 安装Python 3(如果未安装或需要更新)。 - 方法一:从Python官网下载安装器。 - 方法二:使用Homebrew安装。 - 方法三:使用Anaconda(用于数据科学)。 3. 验证安装。 4. 设置虚拟环境(可选,但推荐)。 确保响应是中文。 生成相关问题:在回答结束时,添加相关问题,如“如何设置Python虚拟环境?”等。 响应结构: - 引言:简要说明在MacOS安装Python的重要性。 - 步骤:逐步指导。 - 结尾:添加相关问题。</think>以下是在 macOS安装 Python 的详细步骤指南。macOS 系统通常预装了 Python 2.7(已过时),但现代开发推荐安装 Python 3 的最新版本。本指南将分步介绍两种常用方法:使用官方安装器(适合初学者)和使用 Homebrew(适合高级用户)。所有步骤均基于可靠的参考来源,确保真实可靠。安装前,请确保您的 macOS 系统已更新到最新版本(建议 macOS Catalina 或更高版本)。 ### 步骤 1: 检查系统是否预装 Python安装前,先检查 macOS 是否已预装 Python 或现有版本: 1. 打开终端(在“应用程序” > “实用工具”中找到)。 2. 输入以下命令检查 Python 2.7(系统默认): ```bash python --version ``` 输出可能类似:`Python 2.7.18`(这是旧版本,不建议使用)。 3. 输入以下命令检查 Python 3(如果已安装): ```bash python3 --version ``` - 如果输出类似 `Python 3.x.x`,表示已安装 Python 3;否则,您需要手动安装。 - 参考来源:MacOS 通常预装 Python 2.7,但现代开发需升级到 Python 3[^1]。 ### 步骤 2: 安装 Python 3 推荐两种方法(选择一种即可): #### 方法一:使用官方 Python 安装器(最简单,适合初学者) 1. 访问 [Python 官网下载页](https://www.python.org/downloads/)。 2. 下载最新的 macOS 安装包(例如 Python 3.12.x)。 3. 双击下载的 `.pkg` 文件运行安装程序。 4. 按照向导完成安装(勾选 "Add Python to PATH" 选项,以便在终端直接访问)。 5. 完成后,验证安装: ```bash python3 --version ``` 输出应显示类似 `Python 3.12.x`。 - 参考来源:官方安装器是入门用户的首选,提供一键式安装[^2]。 #### 方法二:使用 Homebrew 安装(适合高级用户,便于管理) Homebrew 是 macOS 的包管理器,能简化安装和更新: 1. 如果未安装 Homebrew,先在终端运行安装命令: ```bash /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` 安装完成后,运行 `brew --version` 确认。 2. 使用 Homebrew 安装 Python 3: ```bash brew install python ``` - 这会安装最新 Python 3 版本。 3. 验证安装: ```bash python3 --version ``` 输出应显示版本号。 - 参考来源:Homebrew 能自动处理依赖项,适合长期维护[^4]。 ### 步骤 3: 可选步骤(提升开发体验) - **设置虚拟环境**:避免依赖冲突,推荐使用 `venv`: ```bash python3 -m venv myenv # 创建虚拟环境 source myenv/bin/activate # 激活环境 ``` - **更新 Python**:如果已安装旧版,可通过 Homebrew 更新: ```bash brew upgrade python ``` - **安装 Anaconda(可选)**:适合数据科学用户,提供预装库(如 NumPy、Pandas)。从 [Anaconda 官网](https://www.anaconda.com/download) 下载 macOS 安装器运行即可。安装后,终端输出类似: ```bash Python 3.11.5 |Anaconda, Inc.| (default, ...) [Clang] on darwin ``` 参考来源:Anaconda 简化了环境管理,适合科学计算[^3]。 ### 常见问题解决 - **权限问题**:如果安装失败,尝试在命令前加 `sudo`(例如 `sudo brew install python`)。 - **PATH 设置**:如果终端无法识别 `python3`,编辑 `~/.zshrc` 文件(或 `~/.bashrc`)添加 `export PATH="/usr/local/bin:$PATH"`,然后运行 `source ~/.zshrc`。 - **卸载旧版本**:不建议删除系统 Python 2.7(macOS 依赖它),但可移除手动安装的版本:对于 Homebrew,运行 `brew uninstall python`;对于官方安装器,使用其卸载工具。 安装完成后,您可以通过运行 `python3` 进入交互模式测试。例如,输入 `print("Hello, Python!")` 验证功能。PythonmacOS安装简单,能快速支持您的编程之旅[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值