ImportError: No module named 'zmq'

Hi guys,

I have a ArtifactInstall_Enter_01_yes.py script as such,

#!/usr/bin/python3

# Import libraries
import os, pwd
import zmq

# draw on default display regardless where run from
# script mandatory for having access to raspberry pi
# missing these lines give 'execution format error'
os.environ["DISPLAY"] = ":0"
pwstruct = pwd.getpwnam("pi")
os.setgid(pwstruct.pw_uid)
os.setuid(pwstruct.pw_gid)

# mandatory to add context to a socket
context = zmq.Context()

# enable socket as REQUEST
socket = context.socket(zmq.REQ)

# Open port for connection and make sure it is not default port like 8888, 50
socket.connect("tcp://localhost:5558")

print("Sending request ...")

# Send message via the port
socket.send(b"New Update")

This gives me the following error in Mender server log
ImportError: No module named ‘zmq’

I have installed zmq from root on my device. What could be the problem ?

Thanks in advance.

Saman

You are explicitly running python3 so you need to make sure that the python3 version of the library is installed.

How did you install the library? Can you share the exact commands used?

Drew

I used
sudo pip3 install zmq.
I have the latest version of the library.

I have tried,

#!/usr/bin/python3
#!/usr/bin/python2
#!/usr/bin/python
#!/usr/bin/env python3

All of them give the same issue.

Hmm. What OS/Distro/Build system are you using? It sounds like something is not right in the pip configuration.

Do you get an error if you just run “python3” manually and then enter “import zmq” at the REPL?

Drew

I am using Raspbian for Raspberry Pi and Mender server is using Ubuntu.

import zmq is working fine from inside python3 on terminal. Using python2 on terminal, I get ‘no module named zmq’. I can’t understand, what’s going on

Sounds like you have the python3 version of zmq but not the python2 version.

I just installed a fresh raspbian and was able to find zmq in both python2 and python3 with the following

pi@raspberrypi:~$ sudo apt update
pi@raspberrypi:~$ sudo apt install python-pip python3-pip
pi@raspberrypi:~$ pip2 install zmq
pi@raspberrypi:~$ pip3 install zmq
pi@raspberrypi:~$ python2
Python 2.7.16 (default, Oct 10 2019, 22:02:15) 
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import zmq
>>> 
pi@raspberrypi:~$ python3
Python 3.7.3 (default, Jul 25 2020, 13:03:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import zmq
>>>