Error in pyenv installing python3.7.0 under Mac OS 10.14

Posted by wscreate on Mon, 16 Dec 2019 17:27:06 +0100

Under Mac OS 10.14, when pyenv is used to install Python 3.7.0, an error is reported, as follows:

xwszt@Linxyz:~$ pyenv install 3.7.0
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.0.tar.xz...
-> https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
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/b_/4ggdxx_539g8cqp2wr2m5sph0000gn/T/python-build.20181012094932.14705
Results logged to /var/folders/b_/4ggdxx_539g8cqp2wr2m5sph0000gn/T/python-build.20181012094932.14705.log

Last 10 log lines:
  File "/private/var/folders/b_/4ggdxx_539g8cqp2wr2m5sph0000gn/T/python-build.20181012094932.14705/Python-3.7.0/Lib/ensurepip/__main__.py", line 5, in <module>
    sys.exit(ensurepip._main())
  File "/private/var/folders/b_/4ggdxx_539g8cqp2wr2m5sph0000gn/T/python-build.20181012094932.14705/Python-3.7.0/Lib/ensurepip/__init__.py", line 204, in _main
    default_pip=args.default_pip,
  File "/private/var/folders/b_/4ggdxx_539g8cqp2wr2m5sph0000gn/T/python-build.20181012094932.14705/Python-3.7.0/Lib/ensurepip/__init__.py", line 117, in _bootstrap
    return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  File "/private/var/folders/b_/4ggdxx_539g8cqp2wr2m5sph0000gn/T/python-build.20181012094932.14705/Python-3.7.0/Lib/ensurepip/__init__.py", line 27, in _run_pip
    import pip._internal
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1

Note the last two lines, indicating zlib could not be found.

Many articles have been found on the Internet, most of which say that the directory / usr/include of zlib should be established. In fact, zlib should be removed from Xcode select -- install in 10.14, so you should install zlib manually. Also use brew installation:

$ brew install zlib
==> Downloading https://homebrew.bintray.com/bottles/zlib-1.2.11.mojave.bottle.t
######################################################################## 100.0%
==> Pouring zlib-1.2.11.mojave.bottle.tar.gz
==> Caveats
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"

==> Summary
  /usr/local/Cellar/zlib/1.2.11: 12 files, 373KB

At the prompt, you also need to set the environment variable:

$ export LDFLAGS="-L/usr/local/opt/zlib/lib"
$ export CPPFLAGS="-I/usr/local/opt/zlib/include"

Then execute the command to install python

$ pyenv install 3.7.0
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.0.tar.xz...
-> https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
Installing Python-3.7.0...
python-build: use readline from homebrew
WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?
Installed Python-3.7.0 to /Users/xwszt/.pyenv/versions/3.7.0

So far, Python 3.7.0 has been installed. Of course, sqlite3 has not been compiled here. This can be installed through brew install sqlite.

Topics: Python zlib brew OpenSSL