如何在Mac OS X Lion上安装Python库'gevent

64 投票
10 回答
40487 浏览
提问于 2025-04-17 03:33

Python库 gevent 的版本是0.13.6(这是在PyPI上的最新版本),在OS X Lion系统上,使用Python 2.7(可能还有其他版本)时,无法通过 pip install 安装。这个库在Snow Leopard系统上可以正常工作。

我该如何安装这个库呢?

如果能通过 pip install 来安装,那就更好了,因为这样可以和自动构建工具很好地配合。

以下是我使用 pip install 时的输出信息:

pip install gevent
Downloading/unpacking gevent
  Running setup.py egg_info for package gevent

Requirement already satisfied (use --upgrade to upgrade): greenlet in ./tl_env/lib/python2.7/site-packages (from gevent)
Installing collected packages: gevent
  Running setup.py install for gevent
    building 'gevent.core' extension
    gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -isysroot /Developer/SDKs/MacOSX10.6.sdk -arch i386 -arch x86_64 -g -O2 -DNDEBUG -g -O3 -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c gevent/core.c -o build/temp.macosx-10.6-intel-2.7/gevent/core.o
    In file included from gevent/core.c:225:
    gevent/libevent.h:9:19: error: event.h: No such file or directory
    gevent/libevent.h:38:20: error: evhttp.h: No such file or directory
    gevent/libevent.h:39:19: error: evdns.h: No such file or directory
    gevent/core.c:361: error: field ‘ev’ has incomplete type
    gevent/core.c:741: warning: parameter names (without types) in function declaration
    gevent/core.c: In function ‘__pyx_f_6gevent_4core___event_handler’:
    gevent/core.c:1619: error: ‘EV_READ’ undeclared (first use in this function)
    gevent/core.c:1619: error: (Each undeclared identifier is reported only once
    gevent/core.c:15376: warning: assignment makes pointer from integer without a cast
   [... about 1000 more lines of compiler errors...]
    gevent/core.c:15385: error: dereferencing pointer to incomplete type
    gevent/core.c: In function ‘__pyx_pf_6gevent_4core_4http___init__’:
    gevent/core.c:15559: warning: assignment makes pointer from integer without a cast
    gevent/core.c: At top level:
    gevent/core.c:21272: error: expected ‘)’ before ‘val’
    lipo: can't figure out the architecture type of: /var/folders/s5/t94kn0p10hdgxzx9_9sprpg40000gq/T//cczk54q7.out
    error: command 'gcc-4.2' failed with exit status 1
    Complete output from command /Users/jacob/code/toplevel/tl_env/bin/python -c "import setuptools;__file__='/Users/jacob/code/toplevel/tl_env/build/gevent/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /var/folders/s5/t94kn0p10hdgxzx9_9sprpg40000gq/T/pip-s2hPd3-record/install-record.txt --install-headers /Users/jacob/code/toplevel/tl_env/bin/../include/site/python2.7:
    running install

running build

running build_py

running build_ext

building 'gevent.core' extension

gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -isysroot /Developer/SDKs/MacOSX10.6.sdk -arch i386 -arch x86_64 -g -O2 -DNDEBUG -g -O3 -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c gevent/core.c -o build/temp.macosx-10.6-intel-2.7/gevent/core.o

10 个回答

18

过了一段时间,我发现上面提到的CFLAGS变量的路径在通过port安装libevent时有效,但在通过brew安装时却不行。以下方法对我在OSX Mavericks上有效:

$ brew install libevent
$ export CFLAGS="-I /usr/local/Cellar/libevent/2.0.21/include -L /usr/local/Cellar/libevent/2.0.21/lib"
$ pip install gevent
24
CFLAGS='-std=c99' pip install gevent

查看内容:无法在OS X 10.11上安装gevent

在OS X 10.11上,clang默认使用c11版本,所以只需要把它改回c99版本就可以了。

114

别把整个内容都发上来!这样太多了!大多数情况下,第一条错误信息就够用了……

gevent/libevent.h:9:19: error: event.h: No such file or directory

这意味着提供 event.h 这个头文件的库没有安装。这个库叫做 libevent(官网)。

一般来说,这种编译错误是构建脚本的问题。构建脚本应该给出一个错误信息,告诉你 libevent 没有安装,如果没有做到这一点,那就是个bug。

你可以通过 MacPorts 来获取 libevent,然后手动告诉编译器用 CFLAGS 环境变量在哪里找到 event.hlibevent,在运行 pip 的时候使用。

sudo port install libevent
CFLAGS="-I /opt/local/include -L /opt/local/lib" pip install gevent

你也可以用 homebrew 来安装 libevent:brew install libevent
(这是 David Wolever 的评论)

撰写回答