3

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
  • 387
  • 1
  • 4
  • 14
  • Can you run `python -c "import sys; print sys.path"` as regular user, and with `sudo` and check if outputs are different? – Ilija Feb 12 '18 at 17:57
  • With `sudo` it says: `['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-arm-linux-gnueabihf', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/gtk-2.0'] ` – sporc Feb 12 '18 at 18:25
  • Without it says: `['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-arm-linux-gnueabihf', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/pi/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/gtk-2.0'] ` – sporc Feb 12 '18 at 18:26

2 Answers2

5

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"

Community
  • 1
  • 1
Maurizio G
  • 66
  • 1
  • This fixed the problem. Thank you. When `paho` was installed for default, how sould it be installed instead? `sudo pip install paho-mqtt` ?? – sporc Feb 14 '18 at 07:14
  • 1
    No, read this: https://stackoverflow.com/questions/9387928/whats-the-difference-between-dist-packages-and-site-packages – Maurizio G Feb 14 '18 at 10:12
1

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

cd <directory>
sudo pip install paho-mqtt -t ./
Raul Perez
  • 35
  • 6