mojave使用pyenv编译python出现 zipimport.ZipImportError: can't decompress data; zlib not available
错误
TL;DR
如果不想看这么啰嗦的博文,直接用下面的命令解决1
2
3
4
5
6
7
8$ sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
$ pyenv install 3.6.6
# Output:
# python-build: use openssl from homebrew
# python-build: use readline from homebrew
# Installing Python-3.6.6...
# python-build: use readline from homebrew
# Installed Python-3.6.6 to /Users/lisupy/.pyenv/versions/3.6.6
起因
听说Mac发布了新版本Mojave,深色主题对我这种熬夜代码狗来说,可谓是真·福利了。迫不及待地我就先升级了,这不就踩上了坑。
使用pyenv编译安装Python的时候出现了下面的错误
1 | python-build: use openssl from homebrew |
这个问题在High Sierra上并没有出现呀。估计就是Mojave的坑了
问题分析
先搜一下有没有人遇到这样的问题
首先有问题先去Github上搜一搜有没有遇到相关的问题,果然有!issue #454,
这个issue上说我没有安装Xcode Command Line Tools
。 胡扯!我明明装了的。1
2lisupy:~ $ xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
brew安装缺失的库
综合上面搜索的结果,Xcode Command Line Tools
会帮我们安装好需要使用的库包括zlib
,但是明明我安装了Xcode Command Line Tools
,还是说缺少zlib
库的头文件,那我就自己装咯。1
lisupy:~ $ brew install zlib
但是homebrew
跟我说我的电脑中已经有了zlib
库了1
2
3
4
5
6
7
8
9
10zlib is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.
For compilers to find zlib you may need to set:
export LDFLAGS="-L/usr/local/opt/zlib/lib"
export CPPFLAGS="-I/usr/local/opt/zlib/include"
For pkg-config to find zlib you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/zlib/lib/pkgconfig"
既然原来有了,编译失败,那么用homebrew
装的自然也就无法编译成功,真实令人头凉。
解决方法
首先查看一下自己xcode-select
的版本1
2lisupy:~ $ xcode-select -v
xcode-select version 2354.
这个版本的xcode-select 似乎在默认情况下不包含Mojave SDK的头文件的,需要手动安装
1 | $ sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target / |
这个似乎是在Mojave发布之后, xcode工具没有赶上步伐,估计在后续版本中会修复的。