mac 下 pyenv 安装python2.7 报错zlib openssl 问题

[sleeli@MacBook-Pro:] ~ $  pyenv install 2.7.5
Downloading openssl-1.0.2k.tar.gz...
-> https://pyenv.github.io/pythons/6b3977c61f2aedf0f96367dcfb5c6e578cf37e7b8d913b4ecb6643c3cb88d8c0
Installing openssl-1.0.2k...


Installed openssl-1.0.2k to /Users/sleeli/.pyenv/versions/2.7.5

python-build: use readline from homebrew
Downloading Python-2.7.5.tar.gz...
-> https://www.python.org/ftp/python/2.7.5/Python-2.7.5.tgz
Installing Python-2.7.5...
patching file ./Modules/readline.c
Hunk #1 succeeded at 204 (offset -2 lines).
Hunk #2 succeeded at 747 (offset -2 lines).
Hunk #3 succeeded at 857 (offset -2 lines).
Hunk #4 succeeded at 905 (offset -13 lines).
patching file ./Lib/site.py
patching file ./Lib/ssl.py
Hunk #2 succeeded at 430 (offset -5 lines).
patching file ./Modules/_ssl.c
Hunk #1 succeeded at 65 (offset -2 lines).
Hunk #2 succeeded at 304 (offset -4 lines).
Hunk #3 succeeded at 1729 (offset -83 lines).
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
ERROR: The Python zlib extension was not compiled. Missing the zlib?

Please consult to the Wiki page to fix the problem.
https://github.com/pyenv/pyenv/wiki/Common-build-problems


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

Inspect or clean up the working tree at /var/folders/p5/pmw3dqqx00gfpzk18dg6m5pr0000gn/T/python-build.20220513102725.20614
Results logged to /var/folders/p5/pmw3dqqx00gfpzk18dg6m5pr0000gn/T/python-build.20220513102725.20614.log

Last 10 log lines:
(cd /Users/sleeli/.pyenv/versions/2.7.5/bin; ln -s python2-config python-config)
test -d /Users/sleeli/.pyenv/versions/2.7.5/lib/pkgconfig || /usr/bin/install -c -d -m 755 /Users/sleeli/.pyenv/versions/2.7.5/lib/pkgconfig
rm -f /Users/sleeli/.pyenv/versions/2.7.5/lib/pkgconfig/python2.pc
(cd /Users/sleeli/.pyenv/versions/2.7.5/lib/pkgconfig; ln -s python-2.7.pc python2.pc)
rm -f /Users/sleeli/.pyenv/versions/2.7.5/lib/pkgconfig/python.pc
(cd /Users/sleeli/.pyenv/versions/2.7.5/lib/pkgconfig; ln -s python2.pc python.pc)
rm -f /Users/sleeli/.pyenv/versions/2.7.5/share/man/man1/python2.1
(cd /Users/sleeli/.pyenv/versions/2.7.5/share/man/man1; ln -s python2.7.1 python2.1)
rm -f /Users/sleeli/.pyenv/versions/2.7.5/share/man/man1/python.1
(cd /Users/sleeli/.pyenv/versions/2.7.5/share/man/man1; ln -s python2.1 python.1)

pyenv在安装其他python版本时总是收到这个zlib构建错误。
我已经尝试了一些网上的办法但是都不太实用,最后找到了一个最全的方法,具体如下:

1.用brew安装zlib openssl 等缺少的库

brew install zlib
brew install openssl
brew install readline

2.安装前添加编译环境变量

export LDFLAGS="-L$(brew --prefix zlib)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix openssl)/lib"
export CPPFLAGS="-I$(brew --prefix zlib)/include -I$(brew --prefix readline)/include -I$(brew --prefix openssl)/include -I$(xcrun --show-sdk-path)/usr/include"

3.最后进行安装

pyenv install 2.7.5

执行过程:

[sleeli@MacBook-Pro:] ~ $ export LDFLAGS="-L$(brew --prefix zlib)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix openssl)/lib"
[sleeli@MacBook-Pro:] ~ $ export CPPFLAGS="-I$(brew --prefix zlib)/include -I$(brew --prefix readline)/include -I$(brew --prefix openssl)/include -I$(xcrun --show-sdk-path)/usr/include"
[sleeli@MacBook-Pro:] ~ $ pyenv install 2.7.5
Downloading openssl-1.0.2k.tar.gz...
-> https://pyenv.github.io/pythons/6b3977c61f2aedf0f96367dcfb5c6e578cf37e7b8d913b4ecb6643c3cb88d8c0
[sleeli@MacBook-Pro:] ~ $ echo $LDFLAGS
-L/usr/local/opt/zlib/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/openssl@3/lib
[sleeli@MacBook-Pro:] ~ $ echo $CPPFLAGS
-I/usr/local/opt/zlib/include -I/usr/local/opt/readline/include -I/usr/local/opt/openssl@3/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include


[sleeli@MacBook-Pro:] ~ $ pyenv install 2.7.5
Downloading openssl-1.0.2k.tar.gz...
-> https://pyenv.github.io/pythons/6b3977c61f2aedf0f96367dcfb5c6e578cf37e7b8d913b4ecb6643c3cb88d8c0
Installing openssl-1.0.2k...
Installed openssl-1.0.2k to /Users/sleeli/.pyenv/versions/2.7.5
python-build: use readline from homebrew
Downloading Python-2.7.5.tar.gz...
-> https://www.python.org/ftp/python/2.7.5/Python-2.7.5.tgz
Installing Python-2.7.5...
patching file ./Modules/readline.c
Hunk #1 succeeded at 204 (offset -2 lines).
Hunk #2 succeeded at 747 (offset -2 lines).
Hunk #3 succeeded at 857 (offset -2 lines).
Hunk #4 succeeded at 905 (offset -13 lines).
patching file ./Lib/site.py
patching file ./Lib/ssl.py
Hunk #2 succeeded at 430 (offset -5 lines).
patching file ./Modules/_ssl.c
Hunk #1 succeeded at 65 (offset -2 lines).
Hunk #2 succeeded at 304 (offset -4 lines).
Hunk #3 succeeded at 1729 (offset -83 lines).
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?
Installing pip from https://bootstrap.pypa.io/pip/2.7/get-pip.py...
Installed Python-2.7.5 to /Users/sleeli/.pyenv/versions/2.7.5

[sleeli@MacBook-Pro:] ~ $ 

你可能感兴趣的:(mac 下 pyenv 安装python2.7 报错zlib openssl 问题)