8

When I use pip freeze on the brew installed version of python 2.7 I get an import error no module named zlib.

➜  ~  pip freeze
Traceback (most recent call last):
  File "/usr/local/bin/pip", line 9, in <module>
    load_entry_point('pip==7.1.2', 'console_scripts', 'pip')()
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 558, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2682, in load_entry_point
    return ep.load()
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2355, in load
    return self.resolve()
  File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2361, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/usr/local/lib/python2.7/site-packages/pip/__init__.py", line 15, in <module>
    from pip.vcs import git, mercurial, subversion, bazaar  # noqa
  File "/usr/local/lib/python2.7/site-packages/pip/vcs/mercurial.py", line 10, in <module>
    from pip.download import path_to_url
  File "/usr/local/lib/python2.7/site-packages/pip/download.py", line 38, in <module>
    from pip._vendor import requests, six
  File "/usr/local/lib/python2.7/site-packages/pip/_vendor/requests/__init__.py", line 58, in <module>
    from . import utils
  File "/usr/local/lib/python2.7/site-packages/pip/_vendor/requests/utils.py", line 26, in <module>
    from .compat import parse_http_list as _parse_list_header
  File "/usr/local/lib/python2.7/site-packages/pip/_vendor/requests/compat.py", line 7, in <module>
    from .packages import chardet
  File "/usr/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/__init__.py", line 3, in <module>
    from . import urllib3
  File "/usr/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/__init__.py", line 10, in <module>
    from .connectionpool import (
  File "/usr/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 38, in <module>
    from .response import HTTPResponse
  File "/usr/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/response.py", line 5, in <module>
    import zlib
ImportError: No module named lib

I think this is normally installed with python but I've installed python (2.7) with brew and an uninstall and reinstall with brew doesn't fix the issue?

➜  ~  which pip
/usr/local/bin/pip
➜  ~  which python
/usr/local/bin/python
bfontaine
  • 18,169
  • 13
  • 73
  • 107
Yunti
  • 6,761
  • 12
  • 61
  • 106
  • It looks like a problem describe here http://stackoverflow.com/a/7587545/4716013 So maybe a wrong sys path and/or a missing `__init__.py` file in appropriate directory... – prodev_paris Sep 17 '15 at 13:54
  • Thanks but i don't see how that question is relevant - that one's about including an __init__.py file so it's recognised as a module. The only thing slightly related I can see is the import error – Yunti Sep 17 '15 at 18:10
  • solved by following this here http://andinfinity.de/quick-note-homebrew-installed-python-fails-to-import-zlib/ – Yunti Sep 21 '15 at 10:18

4 Answers4

7

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.

Christian
  • 113
  • 1
  • 4
  • I get a warning for the force link `Warning: Refusing to link macOS-provided software: zlib` – ahong Dec 29 '18 at 14:16
4

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
Yunti
  • 6,761
  • 12
  • 61
  • 106
1

for me, none of the above worked and I wound up have to link the zlib.h header directly into /usr/

brew install zlib
ln -s /usr/local/Cellar/zlib/1.2.11/include/zlib.h  /usr/local/include/zlib.h

after that, when I tried pip install Pillow==2.2.2 (im working on a old project) ...I finally got the desired "ZLIB (PNG/ZIP) support available" output

  PIL SETUP SUMMARY                                                                                                                                                                                 
  --------------------------------------------------------------------                                                                                                                              
  version      Pillow 2.2.2                                                                                                                                                                         
  platform     darwin 3.5.8 (default, Dec  4 2019, 15:51:38)                                                                                                                                        
               [GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.12)]                                                                                                                        
  --------------------------------------------------------------------                                                                                                                              
  ...
   --- ZLIB (PNG/ZIP) support available    

(macOS Catalina 10.5.3 here)

David Lam
  • 4,689
  • 3
  • 23
  • 34
0

For those people who could not get it to work with the solutions above, use the suggestion from homebrew.

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

The following did nothing for me:

xcode-select --install

or

brew link zlib --force

Instead it gave me an error message:

Warning: Refusing to link macOS-provided software: zlib
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"
ahong
  • 1,041
  • 2
  • 10
  • 22