V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
rizon
V2EX  ›  Python

[已解决] mac python2.7 报错 ImportError: No module named zlib

  •  
  •   rizon ·
    othorizon · 2018-11-26 18:43:44 +08:00 · 4590 次点击
    这是一个创建于 2219 天前的主题,其中的信息可能已经有所发展或是发生改变。

    解决了,用 brew 安装 python@2 时,其中有个警告时需要 apple commandline tools,如果没有他会从源码编译一个 python 出来,所以仍然可以安装上,但是这个是不 适配的,所以安装上命令行工具后(xcode-select --install),再重新装一遍 python 就好了(brew reinstall python@2)

    以下为原帖:


    mac 系统,装了 python2 和 python3,但是执行 python2.7.10 的 pip 命令会报错。

    Traceback (most recent call last):
      File "/usr/local/bin/pip", line 11, in <module>
        load_entry_point('pip==18.0', 'console_scripts', 'pip')()
      File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 484, 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 2714, in load_entry_point
        return ep.load()
      File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2332, in load
        return self.resolve()
      File "/usr/local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2338, in resolve
        module = __import__(self.module_name, fromlist=['__name__'], level=0)
      File "/usr/local/lib/python2.7/site-packages/pip/_internal/__init__.py", line 20, in <module>
        from pip._vendor.urllib3.exceptions import DependencyWarning
      File "/usr/local/lib/python2.7/site-packages/pip/_vendor/urllib3/__init__.py", line 8, in <module>
        from .connectionpool import (
      File "/usr/local/lib/python2.7/site-packages/pip/_vendor/urllib3/connectionpool.py", line 36, in <module>
        from .response import HTTPResponse
      File "/usr/local/lib/python2.7/site-packages/pip/_vendor/urllib3/response.py", line 3, in <module>
        import zlib
    ImportError: No module named zlib
    

    求解怎么办??

    python 是用 homebrew 安装的。

    网上说要安装 zlib 库,可是 mac 系统是自带一个版本的 zlib 的,我没法再装一个其他版本的吧?会冲突吧? 用 brew 安装 zlib 时的提示:

    zlib: stable 1.2.11 (bottled) [keg-only]
    General-purpose lossless data-compression library
    https://zlib.net/
    /usr/local/Cellar/zlib/1.2.11 (12 files, 373KB)
      Poured from bottle on 2018-11-26 at 18:31:13
    From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/zlib.rb
    ==> Caveats
    zlib is keg-only, which means it was not symlinked into /usr/local,
    because macOS already provides this software and installing another version in
    parallel can cause all kinds of trouble.
    
    4 条回复    2018-11-27 11:19:44 +08:00
    likuku
        1
    likuku  
       2018-11-26 19:30:23 +08:00
    不厌其烦再传教一次:pyenv 和 virtualenv 是最棒的~
    Outliver0
        2
    Outliver0  
       2018-11-26 20:09:04 +08:00
    同楼上,搞个虚拟环境美滋滋
    julyclyde
        3
    julyclyde  
       2018-11-27 09:46:57 +08:00
    import 需要的那个东西是 python-zlib
    而不只是 zlib
    rizon
        4
    rizon  
    OP
       2018-11-27 11:19:44 +08:00
    @likuku virtualenv 我也在用,是在项目环境下会使用。不过本机还是会安装 py2 和 py3 两个版本作为机器的基础环境,偶尔会有点用


    @julyclyde 原来如此哈
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1005 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 21:44 · PVG 05:44 · LAX 13:44 · JFK 16:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.