Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import error no module named zlib (brew installed python)

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
like image 757
Yunti Avatar asked Sep 17 '15 12:09

Yunti


3 Answers

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.

like image 105
Christian Avatar answered Oct 09 '22 00:10

Christian


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
like image 42
Yunti Avatar answered Oct 09 '22 01:10

Yunti


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)

like image 1
David Lam Avatar answered Oct 09 '22 01:10

David Lam