ImportError: cannot import name constants

arun6174 picture arun6174 · Sep 18, 2015 · Viewed 23.7k times · Source

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!

Answer

Aazim picture Aazim · Dec 13, 2017

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