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

20,008

Solution 1

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"

Solution 2

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

cd <directory>
sudo pip install paho-mqtt -t ./
Share:
20,008
sporc
Author by

sporc

Updated on October 31, 2020

Comments

  • sporc
    sporc over 3 years

    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?

  • sporc
    sporc over 6 years
    This fixed the problem. Thank you. When paho was installed for default, how sould it be installed instead? sudo pip install paho-mqtt ??
  • Maurizio G
    Maurizio G over 6 years