macOS移除Python2支持

文章目录

升级 macOS Monterey 12.3 之后,我并没有感觉到有什么异样。

直到今天,我想在命令行中打开 vimr:

1zrong@zrong-mbp16$ vimr
2zsh: /usr/local/bin/vimr: bad interpreter: /usr/bin/python: no such file or directory

python 没了

什么?python 没了?

1zrong@zrong-mbp16$  python
2zsh: command not found: python
3zrong@zrong-mbp16$ /usr/bin/python
4zsh: no such file or directory: /usr/bin/python

真没了。

果然,nvim 是使用 /usr/bin/python 启动的:

 1zrong@zrong-mbp16$ nvim $(which vimr)
 2
 3#!/usr/bin/python
 4
 5import urllib
 6import subprocess
 7import argparse
 8import os
 9import uuid
10import json
11.......................

Sunset Python 2

此事早有端倪。

Python 官方早在 2020年 1 月 1 日 就宣布了停止维护 Python 2:

Sunsetting Python 2

We are volunteers who make and take care of the Python programming language. We have decided that January 1, 2020, was the day that we sunset Python 2. That means that we will not improve it anymore after that day, even if someone finds a security problem in it. You should upgrade to Python 3 as soon as you can.

Apple 在 macOS Catalina 10.15 Release Notes 中也提到了 macOS 将不会包含 Python 2.7:

Scripting Language Runtimes

Deprecations

Scripting language runtimes such as Python, Ruby, and Perl are included in macOS for compatibility with legacy software. Future versions of macOS won’t include scripting language runtimes by default, and might require you to install additional packages. If your software depends on scripting languages, it’s recommended that you bundle the runtime within the app. (49764202)

Use of Python 2.7 isn’t recommended as this version is included in macOS for compatibility with legacy software. Future versions of macOS won’t include Python 2.7. Instead, it’s recommended that you run python3 from within Terminal. (51097165)

现在,Apple 在 macOS Monterey 12.3 Release Notes 中直接明确干掉了 Python 2.7 :

Python

Deprecations Python 2.7 was removed from macOS in this update. Developers should use Python 3 or an alternative language instead. (39795874)

pyenv

只要思想不滑坡,办法总比困难多。

无论是使用 conda ,还是 brew ,都可以简单解决这个问题。

然而我还是想用 pyenv 试一试,艺(工具)多不压身嘛对不对?

 1brew install pyenv
 2curl https://pyenv.run | bash
 3brew install openssl readline sqlite3 xz zlib
 4pyenv install 3.10.4
 5python-build: use openssl@1.1 from homebrew
 6python-build: use readline from homebrew
 7Downloading Python-3.10.4.tar.xz...
 8-> https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tar.xz
 9python-build: use openssl@1.1 from homebrew
10python-build: use readline from homebrew
11Installing Python-3.10.4...
12python-build: use tcl-tk from homebrew
13python-build: use readline from homebrew
14python-build: use zlib from xcode sdk
15Installed Python-3.10.4 to /Users/zrong/.pyenv/versions/3.10.4

如果碰到下载太慢无法安装,可以采用下面的流程:

  1. 访问 Python source 网站下载对应的源码包。
  2. 将下载的源码移动到 ~/.pyenv/cache/ 文件夹(若无则手动创建)。
  3. 重新执行安装命令 pyenv install 3.10.4

设定必要的环境变量,设置默认的 python 版本为刚刚安装的 3.10.4。

shims 可以帮助我们解决文章开始提到的没有 python 命令行的问题。

1# 执行更新
2pyenv rehash
3# 写入 shell,这里我使用的是 zsh
4echo 'eval "$(pyenv init --path)"' >> ~/.zprofile
5echo 'eval "$(pyenv init -)"' >> ~/.zshrc
6# shims 让我们可以使用 python/python3/pip 等等命令
7export PATH="$(pyenv root)/shims:$PATH"
8# 设定全局默认 python 版本为 3.10.4
9pyenv global 3.10.4

新开一个 shell,看看 python 的情况:

1zrong@zrong-mbp16$ which python
2/Users/zrong/.pyenv/shims/python
3zrong@zrong-mbp16$ which python3
4/Users/zrong/.pyenv/shims/python3

实际上 python 是什么呢?

1zrong@zrong-mbp16$ cat $(which python)
2#!/usr/bin/env bash
3set -e
4[ -n "$PYENV_DEBUG" ] && set -x
5
6program="${0##*/}"
7
8export PYENV_ROOT="/Users/zrong/.pyenv"
9exec "/usr/local/opt/pyenv/bin/pyenv" exec "$program" "$@"

pyenv 与 conda 的共存

尽管 pyenv 可以管理 conda 的版本,但我并不建议这么做。

因为 pyenv 中安装的 conda 只能为 python 解释器服务。如果在 pyenv 创建的 conda 环境中使用虚拟环境,将依然使用 pyenv virtualenv 而不是使用 conda env create

所以,我建议用这样一套规则来让 pyenv 和 conda 共存。

规则 1:

禁用默认的 conda 环境。

1conda config --set auto_activate_base false
2# 或者在 .condarc 中写入
3auto_activate_base: false

规则 2:

独立使用 pyenv 和 conda 管理自己的环境。

pyenv 与 brew 安装的 python 共存

使用 pyenv 安装了 Python3.9.12 和 3.10.4 之后,我想删除 brew 安装的 python。看了一下 brew 下的 python 版本:

1brew ls | grep python@
2python@3.10
3python@3.9
4python@3.8

但这是不可能的,因为有大量使用 brew 安装的包依赖特定的 brew Python 版本。

1zrong@zrong-mbp16$ brew uninstall python@3.9
2Error: Refusing to uninstall /usr/local/Cellar/python@3.9/3.9.12
3because it is required by cairo, ffmpeg, gdk-pixbuf, glib, gobject-introspection, graphviz, gts, harfbuzz, libass, librsvg, pango and python-tk@3.9, which are currently installed.
4You can override this and force removal with:
5  brew uninstall --ignore-dependencies python@3.9

所以呢,我们要做的,就是什么也不做。

全文完