Mojave使用pyenv安装python-zlib错误

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
python-build: use openssl from homebrew
python-build: use readline from homebrew
Installing Python-3.7.0...
python-build: use readline from homebrew

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

Inspect or clean up the working tree at /var/folders/w2/xwm2lzmn55sfs0_s8gpxv5g00000gn/T/python-build.20181001162025.13689
Results logged to /var/folders/w2/xwm2lzmn55sfs0_s8gpxv5g00000gn/T/python-build.20181001162025.13689.log

Last 10 log lines:
File "/private/var/folders/w2/xwm2lzmn55sfs0_s8gpxv5g00000gn/T/python-build.20181001162025.13689/Python-3.7.0/Lib/ensurepip/__main__.py",
sys.exit(ensurepip._main())
File "/private/var/folders/w2/xwm2lzmn55sfs0_s8gpxv5g00000gn/T/python-build.20181001162025.13689/Python-3.7.0/Lib/ensurepip/__init__.py",
default_pip=args.default_pip,
File "/private/var/folders/w2/xwm2lzmn55sfs0_s8gpxv5g00000gn/T/python-build.20181001162025.13689/Python-3.7.0/Lib/ensurepip/__init__.py",
return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
File "/private/var/folders/w2/xwm2lzmn55sfs0_s8gpxv5g00000gn/T/python-build.20181001162025.13689/Python-3.7.0/Lib/ensurepip/__init__.py",
import pip._internal
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1

这个问题在High Sierra上并没有出现呀。估计就是Mojave的坑了

问题分析

先搜一下有没有人遇到这样的问题

首先有问题先去Github上搜一搜有没有遇到相关的问题,果然有!issue #454,
这个issue上说我没有安装Xcode Command Line Tools。 胡扯!我明明装了的。

1
2
lisupy:~ $ 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
10
zlib 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
2
lisupy:~ $ xcode-select -v
xcode-select version 2354.

这个版本的xcode-select 似乎在默认情况下不包含Mojave SDK的头文件的,需要手动安装

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

这个似乎是在Mojave发布之后, xcode工具没有赶上步伐,估计在后续版本中会修复的。

Author: lisupy
Link: http://lisupy.github.io/2018/10/01/Mojave使用pyenv安装python/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
支付宝打赏
微信打赏