12

I'm trying to convert my python command line application on macOS to an app with py2app.

Every time I try to import zlib or try to install setuptools, I get an error:

No Module named Zlib

Python was installed with brew. I have reinstalled python with brew, I have installed all Xcode CLI related stuff with:

xcode-select --install

I also ran:

ls /usr/include/zlib.h

and I can see that zlib is there where it is supposed to be.

Reinstalled with:

brew reinstall python

Unfortunately nothing has worked. How can I resolve this error?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Luis Valdez
  • 2,339
  • 4
  • 16
  • 21
  • I don't think `/usr/include/zlib.h` is the file used to load the module in python, most C libraries for python have a `.so` extension. For example for me (using python installed from https://www.python.org) `zlib.__file__` points to `/Library/..../lib-dynload/zlib.cpython-35m-darwin.so` – Tadhg McDonald-Jensen Aug 03 '16 at 16:50
  • That's what I found out with my research. I have no idea how to fix is then – Luis Valdez Aug 03 '16 at 17:24

3 Answers3

15

I had the same issue and the solution at https://github.com/Homebrew/homebrew-core/issues/29176 worked for me: re-install python@2:

brew reinstall python@2

(Before I tried this I also tried installing zlib with Homebrew -- brew install zlib; this may or may not have contributed to it working.)

Frans
  • 3,670
  • 1
  • 31
  • 29
  • 3
    This worked for me on Mojave, without needing to do `brew install zlib`. – D Read Nov 23 '18 at 22:20
  • if you install zlib with brew, it's not enough because it's not linked and brew will not link it because there's a platform provided library already that would cause all sorts of issues so you need to tell the compiler where zlib is using `export LDFLAGS="-L/usr/local/opt/zlib/lib"` and `export CPPFLAGS="-I/usr/local/opt/zlib/include"` and also `export PKG_CONFIG_PATH="/usr/local/opt/zlib/lib/pkgconfig"` before you install python with brew or better, with pyenv using something like `pyenv install 3.7.2`. – Paul-Sebastian Manole Mar 28 '19 at 10:06
4

My solution on Mojave (10.14), simply by creating symbolic link. Please take note your zlib and python version may vary.

In terminal run the following:-

brew install zlib

ln -s /usr/local/Cellar/zlib/1.2.11/include/* /usr/local/include

ln -s /usr/local/Cellar/zlib/1.2.11/lib/* /usr/local/lib

brew reinstall python
xwspot
  • 66
  • 4
  • While this fixes the Python issue, it can cause problems if you also need to build iOS apps that rely on zlib, as the homebrew version does not include Arm binaries. – Kevin Nov 13 '18 at 14:01
  • I found that procedure working for Mojave https://holgr.com/fixing-python-zlib-errors-in-macos-mojave-beta/ – François Wauquier Jan 09 '19 at 13:19
4

Installing the MacOS SDK Headers as suggested by this issue solves this problem fairly cleanly.

To do so, run the following (for MacOS 10.14):

xcode-select --install
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

and then brew install python

Thomas BDX
  • 2,632
  • 2
  • 27
  • 31
  • Worked like a charm. The macOS SDK headers package seems to install the required headers needed to compile python. Installing zlib using homebrew and telling the compiler where to find the headers also works: `export LDFLAGS="-L/usr/local/opt/zlib/lib"` and `export CPPFLAGS="-I/usr/local/opt/zlib/include"` and also `export PKG_CONFIG_PATH="/usr/local/opt/zlib/lib/pkgconfig"` before you install python with brew or better, with pyenv using something like `pyenv install 3.7.2`. – Paul-Sebastian Manole Mar 28 '19 at 10:12
  • I'd mark this answer as the correct answer since this uses the platform provided dependencies needed to install python, without bringing in duplicates. – Paul-Sebastian Manole Mar 28 '19 at 10:13
  • 1
    Just a quick note for those whom it is unclear: if python@2 or python are already installed, you'll have to `brew reinstall` them after installing the headers (the 2nd line above). – jennykwan Apr 15 '19 at 14:31