Import Error: paho.mqtt.client not found
Asked Answered
T

2

15

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.

Testis answered 5/1, 2017 at 8:24 Comment(6)
You seem to install it via python3 — are you sure you are staring python3?Alialia
Did you get any warnings installing paho-mqtt? Can you find paho mqtt in you pip list?Ducks
@Alialia I tried with python 2.7 but now the script does not even seem to execute. Should I attach my entire Dockerfile?Testis
@Ducks No I do not get any error or warning during paho mqtt installation.Testis
@Testis I think mqtt is not being installed properly. Could you verify it from the pip list command? What do you get when you run the python --version command? I can install and import it with no problem in Python 3.6.Ducks
@Ducks the pip list shows following list argparse (1.2.1) chardet (2.0.1) colorama (0.2.5) paho-mqtt (1.2) html5lib (0.999) pip (1.5.4) requests (2.2.1) setuptools (3.3) six (1.5.2) urllib3 (1.7.1) virtualenv (1.11.4) wheel (0.24.0) wsgiref (0.1.2) And python version is 2.7Testis
D
27

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.

Ducks answered 5/1, 2017 at 21:42 Comment(4)
Is site packages same as dist packages?Testis
They are essentially the same thing. I don't know why but the Debian based package managers love to use dist-packages instead.Ducks
Thanks a lot. The copy paste thing worked. It is not giving that error.Testis
@Testis Im glad it helped. Please be aware it is not pip friendly to manually copy and paste.Ducks
K
4

You can try install Paho lib:

git clone https://github.com/eclipse/paho.mqtt.python

Once you have the code, it can be installed from your repository as well:

cd paho.mqtt.python
python setup.py install
Khachaturian answered 27/7, 2019 at 3:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.