Luis Valdez
Luis Valdez

Reputation: 2429

How to resolve "No Module named Zlib" error on macOS

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?

Upvotes: 12

Views: 11457

Answers (3)

Thomas BDX
Thomas BDX

Reputation: 2790

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

Upvotes: 4

Frans
Frans

Reputation: 4022

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.)

Upvotes: 15

xwspot
xwspot

Reputation: 66

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

Upvotes: 4

Related Questions