Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Error: paho.mqtt.client not found

I am creating a docker containing python and php. I am writing a python script to connect to a MQTT broker residing in another docker.

In my dockerfile I ensure that I install the paho client by using the following commands:

RUN apt-get install -y python3-dev 
RUN apt-get install -y libffi-dev 
RUN apt-get install -y libssl-dev

ADD https://bootstrap.pypa.io/get-pip.py /tmp/get-pip.py 
RUN cat /tmp/get-pip.py | python3 
RUN pip install paho-mqtt 
RUN pip install python-etcd

However when I run the python script I get the following error:

ImportError: No module named paho.mqtt.client

The docker installation does not show any error with regards to paho-mqtt installation. It will be great if someone can guide on this.

like image 252
Sid411 Avatar asked Jan 05 '17 08:01

Sid411


People also ask

How do I connect to MQTT in Python?

To establish a connection to an MQTT broker using the Python client you use the connect method of the client object. The method can be called with 4 parameters. The connect method declaration is shown below with the default parameters. The only parameter you need to provide is the host name.

What is PAHO MQTT library?

This code provides a client class which enable applications to connect to an MQTT broker to publish messages, and to subscribe to topics and receive published messages. It also provides some helper functions to make publishing one off messages to an MQTT server very straightforward. It supports Python 2.7.


1 Answers

I think I have found the problem,

You have installed Python3 but for some reason the interpreter defaults to version 2.7 in Linux.

Try using pip3 install paho-mqtt python-etcd instead.

Or if it does not work, you can literally copy and paste the paho folder from your Python2.7 site-packages folder to your Python3 site-packages folder. I have just verified paho-mqtt 1.2 for Python2 is exactly the same as paho-mqtt 1.2 for Python3 using a Meld diff tool. Please note, when you directly copy and paste pip list will not display the package you copied.

site-packages are usually inside your system lib folder. It depends upon how Python is installed. In my case everything is inside $HOME/.pyenv folder.

Remember Python2 has it's own site-packages folder and Python3 has it's own site-packages folder where Python searches for the packages. Sometimes if you are using a Debian based Linux distro please make sure to check inside the dist-packages folder as well to see if you can find the package you are looking for.

like image 55
Eddie Avatar answered Sep 21 '22 07:09

Eddie