MacOS M1 安装python3.5

因为没法通过brew直接安装python 3.5,因为brew库里已经没有这个版本的python了,因此只能曲线救国,大体流程:

  • 安装brew
  • 通过brew 安装 pyenv
  • 然后通过pyenv 安装python3.5

问题总出在最后一步,如果直接安装的话,会报错:

» pyenv install 3.5.0
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.5.0.tar.xz...
-> https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tar.xz
Installing Python-3.5.0...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

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

Inspect or clean up the working tree at /var/folders/kp/2xv_4lcd66vcp2f_1_9xdtvc0000gn/T/python-build.20220214165142.54477
Results logged to /var/folders/kp/2xv_4lcd66vcp2f_1_9xdtvc0000gn/T/python-build.20220214165142.54477.log

Last 10 log lines:
Python/random.c:88:19: note: did you mean 'py_getentropy'?
Python/random.c:80:1: note: 'py_getentropy' declared here
py_getentropy(unsigned char *buffer, Py_ssize_t size, int fatal)
^
Python/random.c:97:19: error: implicit declaration of function 'getentropy' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
            res = getentropy(buffer, len);
                  ^
2 errors generated.
make: *** [Python/random.o] Error 1
make: *** Waiting for unfinished jobs....

或者:

» pyenv install 3.5.10                                                                                                                                                                                        1 ↵
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.5.10.tar.xz...
-> https://www.python.org/ftp/python/3.5.10/Python-3.5.10.tar.xz
Installing Python-3.5.10...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

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

Inspect or clean up the working tree at /var/folders/kp/2xv_4lcd66vcp2f_1_9xdtvc0000gn/T/python-build.20220214171051.99844
Results logged to /var/folders/kp/2xv_4lcd66vcp2f_1_9xdtvc0000gn/T/python-build.20220214171051.99844.log

Last 10 log lines:
    ^~~~~~~~~~~~~
./Modules/posixmodule.c:11345:9: warning: code will never be executed [-Wunreachable-code]
        posix_error();
        ^~~~~~~~~~~
1 warning generated.
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include   -I/Library/Developer/CommandLineTools/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/openssl@1.1/include -I/Users/youzhengchuan/.pyenv/versions/3.5.10/include  -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/opt/openssl@1.1/include -I/Users/youzhengchuan/.pyenv/versions/3.5.10/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.

查了半边天,用如下方法安装即可:

  • 首先确认你系统安装的openssl,比如我当前系统有2个版本openssl:
 » brew list | grep openssl
openssl@1.1
openssl@3
  • 然后编辑一个安装脚本,注意替换里面的openssl为你系统的openssl
» cat ~/python-3.5-install.sh
#!/bin/bash

set -ex

export CFLAGS="-I$(brew --prefix openssl@1.1)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include"
export LDFLAGS="-L$(brew --prefix openssl@1.1)/lib -L$(brew --prefix readline)/lib -L$(xcrun --show-sdk-path)/usr/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib"
export CPPFLAGS="-I/usr/local/opt/zlib/include"
export PKG_CONFIG_PATH="/usr/local/opt/zlib/lib/pkgconfig"

#pyenv install --patch 3.5.9 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch)

#export LDFLAGS="-L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib"

arch -x86_64 pyenv install --patch 3.5.10 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

然后直接执行这个脚本即可:

» bash ~/python-3.5-install.sh
++ brew --prefix openssl@1.1
++ brew --prefix readline
++ xcrun --show-sdk-path
+ export 'CFLAGS=-I/usr/local/opt/openssl@1.1/include -I/usr/local/opt/readline/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include'
+ CFLAGS='-I/usr/local/opt/openssl@1.1/include -I/usr/local/opt/readline/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include'
++ brew --prefix openssl@1.1
++ brew --prefix readline
++ xcrun --show-sdk-path
++ brew --prefix zlib
++ brew --prefix bzip2
+ export 'LDFLAGS=-L/usr/local/opt/openssl@1.1/lib -L/usr/local/opt/readline/lib -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -L/usr/local/opt/zlib/lib -L/usr/local/opt/bzip2/lib'
+ LDFLAGS='-L/usr/local/opt/openssl@1.1/lib -L/usr/local/opt/readline/lib -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -L/usr/local/opt/zlib/lib -L/usr/local/opt/bzip2/lib'
+ export CPPFLAGS=-I/usr/local/opt/zlib/include
+ CPPFLAGS=-I/usr/local/opt/zlib/include
+ export PKG_CONFIG_PATH=/usr/local/opt/zlib/lib/pkgconfig
+ PKG_CONFIG_PATH=/usr/local/opt/zlib/lib/pkgconfig
+ arch -x86_64 pyenv install --patch 3.5.10
++ curl -sSL 'https://github.com/python/cpython/commit/8ea6353.patch?full_index=1'
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.5.10.tar.xz...
-> https://www.python.org/ftp/python/3.5.10/Python-3.5.10.tar.xz
Installing Python-3.5.10...
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.10 to /Users/youzhengchuan/.pyenv/versions/3.5.10

 » /Users/youzhengchuan/.pyenv/versions/3.5.10/bin/python -V
Python 3.5.10
posted @ 2022-02-14 17:31  川川籽  阅读(1415)  评论(1编辑  收藏  举报