简体   繁体   中英

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

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?

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"

A quick solution is to install the library in the specific directory

cd <directory>
sudo pip install paho-mqtt -t ./

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM