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

2018-11-26 18:43:44 +08:00
 rizon

解决了,用 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.
4572 次点击
所在节点    Python
4 条回复
likuku
2018-11-26 19:30:23 +08:00
不厌其烦再传教一次:pyenv 和 virtualenv 是最棒的~
Outliver0
2018-11-26 20:09:04 +08:00
同楼上,搞个虚拟环境美滋滋
julyclyde
2018-11-27 09:46:57 +08:00
import 需要的那个东西是 python-zlib
而不只是 zlib
rizon
2018-11-27 11:19:44 +08:00
@likuku virtualenv 我也在用,是在项目环境下会使用。不过本机还是会安装 py2 和 py3 两个版本作为机器的基础环境,偶尔会有点用


@julyclyde 原来如此哈

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/511697

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX