Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 774
arun6174 Avatar asked Sep 18 '15 03:09

arun6174


2 Answers

so, what worked for me in this case is the following solution from Osiris from letsencrypt forum:

  1. the problem is that you have 2 versions of certbot (in my case I understood that by command:

whereis certbot

the output was like this:

certbot: /usr/bin/certbot /usr/local/bin/certbot /usr/share/man/man1/certbot.1.gz

  1. then I tried to renew both of them wiht the commands:

/usr/bin/certbot renew
/usr/local/bin/certbot renew

and I got the error:
An unexpected error occurred: ImportError: cannot import name 'constants'
...with the 2nd one!
so, I used the following command to remove this folder:
sudo rm /usr/local/bin/certbot

Then the error was gone
Hope that helps

like image 63
Max Avatar answered Nov 04 '22 21:11

Max


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

like image 43
Aazim Avatar answered Nov 04 '22 21:11

Aazim