【问题标题】:Cannot import zmq in python (install issue)无法在 python 中导入 zmq(安装问题)
【发布时间】:2014-04-06 22:07:45
【问题描述】:

我似乎无法在我的 macbook (OSX 10.9.1) 上安装 pyzmq

第一次调用是运行:

sudo pip install pyzmq

出现找不到libzmq的错误,似乎尝试编译捆绑版本:

jono@air:~ $ sudo pip install pyzmq
Password:
Downloading/unpacking pyzmq
  Downloading pyzmq-14.0.1.tar.gz (867kB): 867kB downloaded
  Running setup.py egg_info for package pyzmq

    no previously-included directories found matching 'docs/build'
    no previously-included directories found matching 'docs/gh-pages'
    warning: no directories found matching 'bundled/uuid'
    warning: no previously-included files found matching 'bundled/uuid/Makefile*'
    warning: no previously-included files found matching 'bundled/zeromq/src/Makefile*'
    warning: no previously-included files found matching 'bundled/zeromq/src/platform.hpp'
    warning: no previously-included files found matching 'setup.cfg'
    warning: no previously-included files found matching 'zmq/libzmq*'
    warning: no previously-included files matching '__pycache__/*' found anywhere in distribution
    warning: no previously-included files matching '.deps/*' found anywhere in distribution
    warning: no previously-included files matching '*.so' found anywhere in distribution
    warning: no previously-included files matching '*.pyd' found anywhere in distribution
    warning: no previously-included files matching '.git*' found anywhere in distribution
    warning: no previously-included files matching '.DS_Store' found anywhere in distribution
    warning: no previously-included files matching '.mailmap' found anywhere in distribution
Installing collected packages: pyzmq
  Running setup.py install for pyzmq
    Did not find libzmq via pkg-config:
    Package libzmq was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libzmq.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'libzmq' found

    xcrun clang -fno-strict-aliasing -fno-common -dynamic -I/usr/local/include -I/usr/local/opt/sqlite/include -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -c build/temp.macosx-10.8-x86_64-2.7/scratch/check_sys_un.c -o build/temp.macosx-10.8-x86_64-2.7/scratch/check_sys_un.o
    xcrun clang -undefined dynamic_lookup build/temp.macosx-10.8-x86_64-2.7/scratch/check_sys_un.o -o build/temp.macosx-10.8-x86_64-2.7/scratch/check_sys_un
    Configure: Autodetecting ZMQ settings...
        Custom ZMQ dir:
    ************************************************
    xcrun clang -fno-strict-aliasing -fno-common -dynamic -I/usr/local/include -I/usr/local/opt/sqlite/include -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Izmq/utils -Izmq/backend/cython -Izmq/devices -c build/temp.macosx-10.8-x86_64-2.7/scratch/vers.c -o build/temp.macosx-10.8-x86_64-2.7/scratch/vers.o
    build/temp.macosx-10.8-x86_64-2.7/scratch/vers.c:4:10: fatal error: 'zmq.h' file not found
    #include "zmq.h"
             ^
    1 error generated.

    error: command 'xcrun' failed with exit status 1

    Failed with default libzmq, trying again with /usr/local
    Configure: Autodetecting ZMQ settings...
        Custom ZMQ dir:       /usr/local
    ************************************************
    xcrun clang -fno-strict-aliasing -fno-common -dynamic -I/usr/local/include -I/usr/local/opt/sqlite/include -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/include -Izmq/utils -Izmq/backend/cython -Izmq/devices -c build/temp.macosx-10.8-x86_64-2.7/scratch/vers.c -o build/temp.macosx-10.8-x86_64-2.7/scratch/vers.o
    build/temp.macosx-10.8-x86_64-2.7/scratch/vers.c:4:10: fatal error: 'zmq.h' file not found
    #include "zmq.h"
             ^
    1 error generated.

    error: command 'xcrun' failed with exit status 1

    Warning: Failed to build or run libzmq detection test.

    If you expected pyzmq to link against an installed libzmq, please check to make sure:

        * You have a C compiler installed
        * A development version of Python is installed (including headers)
        * A development version of ZMQ >= 2.1.4 is installed (including headers)
        * If ZMQ is not in a default location, supply the argument --zmq=<path>
        * If you did recently install ZMQ to a default location,
          try rebuilding the ld cache with `sudo ldconfig`
          or specify zmq's location with `--zmq=/usr/local`

    If you expected to get a binary install (egg), we have those for
    current Pythons on OS X and Windows. These can be installed with
    easy_install, but PIP DOES NOT SUPPORT EGGS.

    You can skip all this detection/waiting nonsense if you know
    you want pyzmq to bundle libzmq as an extension by passing:

        `--zmq=bundled`

    I will now try to build libzmq as a Python extension
    unless you interrupt me (^C) in the next 10 seconds...

    ************************************************
 1...
    Using bundled libzmq
    already have bundled/zeromq
    attempting ./configure to generate platform.hpp
    Warning: failed to configure libzmq:
    /bin/sh: ./configure: No such file or directory

    staging platform.hpp from: buildutils/include_darwin
    ************************************************
    cc -c /tmp/crypto_box_keypairpmO9ft.c -o build/temp.macosx-10.8-x86_64-2.7/tmp/crypto_box_keypairpmO9ft.o
    /tmp/crypto_box_keypairpmO9ft.c:1:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
    main (int argc, char **argv) {
    ^~~~
    /tmp/crypto_box_keypairpmO9ft.c:2:5: warning: implicit declaration of function 'crypto_box_keypair' is invalid in C99 [-Wimplicit-function-declaration]
        crypto_box_keypair();
        ^
    2 warnings generated.
    cc build/temp.macosx-10.8-x86_64-2.7/tmp/crypto_box_keypairpmO9ft.o -lsodium -o build/temp.macosx-10.8-x86_64-2.7/a.out
    ld: library not found for -lsodium
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    Warning: libsodium not found, zmq.CURVE security will be unavailable
    ************************************************
    building 'zmq.libzmq' extension
    xcrun clang -fno-strict-aliasing -fno-common -dynamic -I/usr/local/include -I/usr/local/opt/sqlite/include -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Ibundled/zeromq/include -Ibundled -I/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c buildutils/initlibzmq.c -o build/temp.macosx-10.8-x86_64-2.7/buildutils/initlibzmq.o
...
... (lots more compiling)
...
    xcrun clang -bundle -undefined dynamic_lookup -L/usr/local/lib -L/usr/local/opt/sqlite/lib -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk build/temp.macosx-10.8-x86_64-2.7/zmq/backend/cython/utils.o -o build/lib.macosx-10.8-x86_64-2.7/zmq/backend/cython/utils.so

    no previously-included directories found matching 'docs/build'
    no previously-included directories found matching 'docs/gh-pages'
    warning: no directories found matching 'bundled/uuid'
    warning: no previously-included files found matching 'bundled/uuid/Makefile*'
    warning: no previously-included files found matching 'bundled/zeromq/src/Makefile*'
    warning: no previously-included files found matching 'setup.cfg'
    warning: no previously-included files found matching 'zmq/libzmq*'
    warning: no previously-included files matching '__pycache__/*' found anywhere in distribution
    warning: no previously-included files matching '.deps/*' found anywhere in distribution
    warning: no previously-included files matching '*.so' found anywhere in distribution
    warning: no previously-included files matching '*.pyd' found anywhere in distribution
    warning: no previously-included files matching '.git*' found anywhere in distribution
    warning: no previously-included files matching '.DS_Store' found anywhere in distribution
    warning: no previously-included files matching '.mailmap' found anywhere in distribution
Successfully installed pyzmq
Cleaning up...

这不起作用:

jono@air:~ $ python
Python 2.7.3 (default, Mar 15 2013, 22:53:11) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import zmq
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/zmq/__init__.py", line 35, in <module>
    _libzmq = ctypes.CDLL(bundled[0], mode=ctypes.RTLD_GLOBAL)
  File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: dlopen(/usr/local/lib/python2.7/site-packages/zmq/libzmq.so, 10): Symbol not found: __ZNSiD0Ev
  Referenced from: /usr/local/lib/python2.7/site-packages/zmq/libzmq.so
  Expected in: flat namespace
 in /usr/local/lib/python2.7/site-packages/zmq/libzmq.so
>>> 

所以我将安装过程中的错误解释为没有 libzmq。按照https://github.com/zeromq/pyzmq/wiki/Building-and-Installing-PyZMQ的说明,我卸载了它,然后尝试:

jono@air:~ $ brew install zeromq
==> Downloading http://download.zeromq.org/zeromq-3.2.3.tar.gz
######################################################################## 100.0%
==> Patching
patching file tests/test_disconnect_inproc.cpp
==> ./configure --prefix=/usr/local/Cellar/zeromq/3.2.3
==> make
==> make install
==> Caveats
To install the zmq gem on 10.6 with the system Ruby on a 64-bit machine,
you may need to do:

    ARCHFLAGS="-arch x86_64" gem install zmq -- --with-zmq-dir=/usr/local/opt/zeromq
==> Summary
????  /usr/local/Cellar/zeromq/3.2.3: 54 files, 2.4M, built in 52 seconds

接着是:

jono@air:~ $ sudo pip install pyzmq
Password:
Downloading/unpacking pyzmq
  Running setup.py egg_info for package pyzmq

    no previously-included directories found matching 'docs/build'
    no previously-included directories found matching 'docs/gh-pages'
    warning: no directories found matching 'bundled/uuid'
    warning: no previously-included files found matching 'bundled/uuid/Makefile*'
    warning: no previously-included files found matching 'bundled/zeromq/src/Makefile*'
    warning: no previously-included files found matching 'setup.cfg'
    warning: no previously-included files found matching 'zmq/libzmq*'
    warning: no previously-included files matching '__pycache__/*' found anywhere in distribution
    warning: no previously-included files matching '.deps/*' found anywhere in distribution
    warning: no previously-included files matching '*.so' found anywhere in distribution
    warning: no previously-included files matching '*.pyd' found anywhere in distribution
    warning: no previously-included files matching '.git*' found anywhere in distribution
    warning: no previously-included files matching '.DS_Store' found anywhere in distribution
    warning: no previously-included files matching '.mailmap' found anywhere in distribution
Installing collected packages: pyzmq
  Running setup.py install for pyzmq
    Using bundled libzmq
    already have bundled/zeromq
    already have platform.hpp
    ************************************************
    cc -c /tmp/crypto_box_keypairLbTEQR.c -o build/temp.macosx-10.8-x86_64-2.7/tmp/crypto_box_keypairLbTEQR.o
    /tmp/crypto_box_keypairLbTEQR.c:1:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
    main (int argc, char **argv) {
    ^~~~
    /tmp/crypto_box_keypairLbTEQR.c:2:5: warning: implicit declaration of function 'crypto_box_keypair' is invalid in C99 [-Wimplicit-function-declaration]
        crypto_box_keypair();
        ^
    2 warnings generated.
    cc build/temp.macosx-10.8-x86_64-2.7/tmp/crypto_box_keypairLbTEQR.o -lsodium -o build/temp.macosx-10.8-x86_64-2.7/a.out
    ld: library not found for -lsodium
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    Warning: libsodium not found, zmq.CURVE security will be unavailable
    ************************************************

    no previously-included directories found matching 'docs/build'
    no previously-included directories found matching 'docs/gh-pages'
    warning: no directories found matching 'bundled/uuid'
    warning: no previously-included files found matching 'bundled/uuid/Makefile*'
    warning: no previously-included files found matching 'bundled/zeromq/src/Makefile*'
    warning: no previously-included files found matching 'setup.cfg'
    warning: no previously-included files found matching 'zmq/libzmq*'
    warning: no previously-included files matching '__pycache__/*' found anywhere in distribution
    warning: no previously-included files matching '.deps/*' found anywhere in distribution
    warning: no previously-included files matching '*.so' found anywhere in distribution
    warning: no previously-included files matching '*.pyd' found anywhere in distribution
    warning: no previously-included files matching '.git*' found anywhere in distribution
    warning: no previously-included files matching '.DS_Store' found anywhere in distribution
    warning: no previously-included files matching '.mailmap' found anywhere in distribution
Successfully installed pyzmq
Cleaning up...

我不断收到错误:

>>> import zmq
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/zmq/__init__.py", line 35, in <module>
    _libzmq = ctypes.CDLL(bundled[0], mode=ctypes.RTLD_GLOBAL)
  File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: dlopen(/usr/local/lib/python2.7/site-packages/zmq/libzmq.so, 10): Symbol not found: __ZNSiD0Ev
  Referenced from: /usr/local/lib/python2.7/site-packages/zmq/libzmq.so
  Expected in: flat namespace
 in /usr/local/lib/python2.7/site-packages/zmq/libzmq.so

不管我做什么。

【问题讨论】:

  • MacPorts 在安装/更新 pyzmq 时非常适合我,至少在 10.8 上。设置可能需要一些时间,但如果你真的需要这个库,它可能是值得的......
  • 在我的旧机器上,我尝试同时使用 macports 和 homebrew,并且非常头疼。下次我必须在 homebrew 和 macports 之间做出决定时会记住;)
  • 当我第一次得到我的 Mac 时,我遇到了同样的问题,并决定使用 Macports,只是因为他们提供了大量的软件包,并且他们保持最新的速度新版本。与 brew 不同,我几乎从来没有遇到过依赖问题。我猜对每个人来说都是:)

标签: python macos installation zeromq pyzmq


【解决方案1】:

经过大量努力,我回到了基础并设法通过以下方式使其工作:

从源代码构建:

brew install zeromq
...
python setup.py configure --zmq=/usr/local/Cellar/zeromq/4.0.3/
...
sudo python setup.py install
...

sudo port install 
...

我不知道为什么标准方法不起作用,但至少我有 pyzmq 工作:)

【讨论】:

    【解决方案2】:

    在 OSX/MacPorts 中遇到了类似的 ImportError 异常,使用 sudo port install zeromq 安装 ZeroMQ 库为我修复了它。

    【讨论】:

      【解决方案3】:

      花了很多时间解决这个问题,终于这对我有用:

      • brew install zeromq

      • 找到隐藏libzmq.pc的污点(我的例子是:Applications/Canopy.app/appdata/canopy-1.4.1.1975.macosx-x86_64/Canopy.app/Contents/lib/pkgconfig

      • 然后制作:

        export PKG_CONFIG_PATH=<path_found_above>
        

        以我为例:

        export PKG_CONFIG_PATH=/Applications/Canopy.app/appdata/canopy-1.4.1.1975.macosx-x86_64/Canopy.app/Contents/lib/pkgconfig
        

      【讨论】:

      • 欢迎堆栈溢出!感谢您的贡献。所有第一篇文章都进行了审查,我看到了这个。我做了一些编辑以适应网站的格式风格 - 请随时 check them 了解我是如何获得代码块格式等的。
      【解决方案4】:

      我认为问题在于安装的 python 是用“--enable-unicode=ucs4”编译的,

      >>> import sysconfig
      >>> sysconfig.get_config_var('Py_UNICODE_SIZE')
      >>> 4
      

      https://github.com/yyuu/pyenv/issues/257#issuecomment-195836492 中描述了重新编译 python 的解决方案 如果你使用 pyenv。

      $pyenv uninstall 2.7.11
      $PYTHON_CONFIGURE_OPTS="--enable-unicode=ucs2" pyenv install 2.7.11
      

      然后重新创建您的 virtualenv,重新安装每个包...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-21
        • 2015-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多