Import error no module named zlib (brew installed python)

For macOs 10.14 mojave users with Xcode-beta installed the following should work as xcode-select --install doesn't seem to supply the missing header files, at least not in a location that works for installing python via brew...

What worked for me is as follows:

brew install zlib
brew link zlib --force

#python 3
brew (re)install python3
brew postinstall python3
brew link python3 #just in case...

#python 2
brew (re)install python2
brew link python2 #just in case...

In my case I also had to reinstall some of my python modules previously installed via pip.


This is an issue with xcode not installing zlib properly.

Install the xcode CLI with:

xcode-select --install

Then before reinstalling Python with brew I check if the zlib header is where brew is looking for it via the terminal :

ls /usr/include/zlib.h 

Then reinstall python via brew:

brew reinstall python