Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

python - ImportError: cannot import name constants

I'm trying to run a simple piece of code using pyzmq. I am using Python 2.7 and pyzmq 14.5

$ python --version
Python 2.7.6
$ sudo find /usr -name "*pyzmq*"
/usr/local/lib/python2.7/dist-packages/pyzmq-14.5.0.egg-info
/usr/lib/python2.7/dist-packages/pyzmq-14.0.1.egg-info

Following is the code i'm trying to run:

import zhelpers

context = zmq.Context.instance()
server = context.socket(zmq.ROUTER)
server.bind("tcp://*:5678")

while (1):
    address, empty, data = server.recv_multipart()

    print("address = %s, data = %d" % (address, int(data)))

    data_i = int(data) + 10
    server.send_multipart([
        address,
        b'',
        str(data_i),
    ])

But, I'm getting following error and got no clue how to fix this:

Traceback (most recent call last):
  File "reqrep_server.py", line 8, in <module>
    import zhelpers
  File "/home/arun/pyzmq_server/zhelpers.py", line 11, in <module> 
    import zmq
  File "/home/arun/pyzmq_server/zmq/__init__.py", line 66, in <module>
    from zmq import backend
  File "/home/arun/pyzmq_server/zmq/backend/__init__.py", line 41, in <module>
    reraise(*exc_info)
  File "/home/arun/pyzmq_server/zmq/backend/__init__.py", line 29, in <module>
    _ns = select_backend(first)
  File "/home/arun/pyzmq_server/zmq/backend/select.py", line 27, in select_backend
    mod = __import__(name, fromlist=public_api)
  File "/home/arun/pyzmq_server/zmq/backend/cython/__init__.py", line 6, in <module>
    from . import (constants, error, message, context, socket, utils, _poll, _version, _device)
ImportError: cannot import name constants

I've copied the whole zmq folder and placed it in the level as my .py file.

Please help!

EDIT:

I've removed those two versions of pyzmq and reinstalled latest pyzmq (with libzmq bundled this time) as instructed here.

$ sudo find /usr -name "*pyzmq*"
/usr/local/lib/python2.7/dist-packages/pyzmq-14.7.0-py2.7.egg-info

$ sudo find /usr -name "*libzmq*"
/usr/local/lib/libzmq.so
/usr/local/lib/libzmq.la
/usr/local/lib/libzmq.so.5.0.0
/usr/local/lib/pkgconfig/libzmq.pc
/usr/local/lib/libzmq.so.5
/usr/local/lib/python2.7/dist-packages/zmq/libzmq.so
/usr/local/lib/python2.7/dist-packages/zmq/backend/cython/libzmq.pxd
/usr/local/lib/libzmq.a

But this doesn't solve the problem. I'm getting the same error!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I encountered a similar problem. pip install --upgrade pyzmq did the trick for me


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...