ImportError: cannot import name constants
Asked Answered
P

3

14

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!

Pollitt answered 18/9, 2015 at 3:30 Comment(4)
It looks like you have two versions installed. Try starting over. Remove all pyzmq packages and re-install it (using pip or whatever).Kweichow
DON'T copy any Python bundle into your code. It is structured in a specific way and it follows very strict and complex rules when importing. That won't definitely solve the problem, even though it could have looked like so. The error must be otherwhere.Aileenailene
Glad you solved your problem! You should submit an answer and, when allowed, accept it, so future readers know the problem is solved.Sorus
How did you solve your problem?Theorist
T
20

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

Twoedged answered 13/12, 2017 at 19:27 Comment(2)
Not for me, unfortunatelyPantie
ya not worked for me either, anybody found the solution for this?Whap
S
4

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

Scabble answered 3/10, 2020 at 11:34 Comment(1)
It works for me when I use the command: sudo rm /usr/local/bin/certbotMylo
A
0

The following worked for me.

pip install --upgrade pyzmq --user

--user is to check for the permissions

Ache answered 1/9, 2021 at 23:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.