Import error no module named zlib (brew installed

2019-04-26 18:45发布

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

3条回答
老娘就宠你
2楼-- · 2019-04-26 19:12

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.

查看更多
一夜七次
3楼-- · 2019-04-26 19:20

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
查看更多
贪生不怕死
4楼-- · 2019-04-26 19:36

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"
查看更多
登录 后发表回答