Cannot run paho mqtt client -> "ImportError: No module named paho.mqtt.client"

sporc picture sporc · Feb 12, 2018 · Viewed 17k times · Source

I am trying to set up an mqtt client, but I am getting an ImportError:

I installed paho with:

pip install paho-mqtt

It said: Successfully installed dnspython-1.15.0 paho-mqtt-1.3.1 python-etcd-0.4.5 urllib3-1.22

But when I am trying to call my python script with sudo python listen.py it says:

File "listen.py", line 6, in <module>
  import paho.mqtt.client as mqtt
ImportError: No module named paho.mqtt.client

When I am typing python --version it says: Python 2.7.13 and when I call pip freeze paho is listed paho-mqtt==1.3.1

Any suggestions what's wrong?

Answer

Maurizio G picture Maurizio G · Feb 13, 2018

The problem is that the library "paho" has been installed (for default) in the folder "/home/pi/.local/lib/python2.7/site-packages" but "sudo python" search this library in the folder "/usr/local/lib/python2.7/dist-packages". I have solved with one link:

cd /usr/lib/python2.7/dist-packages

sudo ln -s /home/pi/.local/lib/python2.7/site-packages/paho

Now my script run also with "sudo"