macOS Big Sur使用Pyenv安装python失败

最近需要维护一份python程序,开发时的环境是Python3.5.6,但是我本地环境是Python3.8.9,虽然都是Python3,但是程序的确运行不起来,所以我需要通过Pyenv来安装其他版本的Python。

安装

Pyenv 是 Python 版本管理工具。 Pyenv 可以改变全局的 Python 版本,在系统中安装多个版本的 Python, 设置目录级别的 Python 版本,还能创建和管理 virtual python environments。通常使用如下命令即可安装不同的版本。

1
pyenv install 3.5.6

这个命令之前(2019年)一直是好用的,后来一段时间再没去开发Python程序所以就再没用过。现在这个命令却是不好使了。

报错提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  ~  pyenv install 3.5.6
python-build: use [email protected] from homebrew
python-build: use readline from homebrew
Downloading Python-3.5.6.tar.xz...
-> https://www.python.org/ftp/python/3.5.6/Python-3.5.6.tar.xz
Installing Python-3.5.6...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

BUILD FAILED (OS X 11.3 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/h6/h0z030p94js_t1nx_m7tpyz00000gn/T/python-build.20210621110910.12796
Results logged to /var/folders/h6/h0z030p94js_t1nx_m7tpyz00000gn/T/python-build.20210621110910.12796.log

Last 10 log lines:
Py_FatalError("abort() called from Python code didn't abort!");
^~~~~~~~~~~~~
./Modules/posixmodule.c:11345:9: warning: code will never be executed [-Wunreachable-code]
posix_error();
^~~~~~~~~~~
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -Werror=declaration-after-statement -I. -I./Include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/opt/[email protected]/include -I/Users/liyang/.pyenv/versions/3.5.6/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/opt/[email protected]/include -I/Users/liyang/.pyenv/versions/3.5.6/include -DPy_BUILD_CORE -c ./Modules/_sre.c -o Modules/_sre.o
11 warnings and 1 error generated.
make: *** [Modules/posixmodule.o] Error 1
make: *** Waiting for unfinished jobs....
1 warning generated.

解决方案

可使用如下方式进行安装。本次以安装Python3.5.6为例,如需其他版本,可自行修改版本号即可,其他保持不变。

1
CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" pyenv install --patch 3.5.6 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

效果如下:

1
2
3
4
5
6
7
8
9
10
11
python-build: use [email protected] from homebrew
python-build: use readline from homebrew
Installing Python-3.5.6...
patching file Misc/NEWS.d/next/macOS/2020-06-24-13-51-57.bpo-41100.mcHdc5.rst
patching file configure
Hunk #1 succeeded at 3492 (offset 66 lines).
patching file configure.ac
Hunk #1 succeeded at 492 (offset -18 lines).
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.5.6 to /Users/liyang/.pyenv/versions/3.5.6

使用查看目前已安装的版本。

1
2
3
4
 [email protected]  ~  pyenv versions
system
3.5.6
* 3.6.13 (set by /Users/liyang/.pyenv/version)

参考链接: