Python 导入错误:未找到 paho.mqtt.client

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41480256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:02:36  来源:igfitidea点击:

Import Error: paho.mqtt.client not found

pythondockermqttpaho

提问by Sid411

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.

我正在创建一个包含 python 和 php 的 docker。我正在编写一个 python 脚本来连接到驻留在另一个 docker 中的 MQTT 代理。

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

在我的 dockerfile 中,我确保使用以下命令安装了 paho 客户端:

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:

但是,当我运行 python 脚本时,出现以下错误:

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.

docker 安装未显示有关 paho-mqtt 安装的任何错误。如果有人可以对此进行指导,那就太好了。

回答by Eddie

I think I have found the problem,

我想我已经找到了问题,

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

您已经安装,Python3但由于某种原因,解释器在 Linux 中默认为 2.7 版。

Try using pip3 install paho-mqtt python-etcdinstead.

尝试使用pip3 install paho-mqtt python-etcd

Or if it does not work, you can literally copy and paste the pahofolder from your Python2.7site-packagesfolder to your Python3site-packagesfolder. I have just verified paho-mqtt 1.2for Python2is exactly the same as paho-mqtt 1.2for Python3using a Melddiff tool. Please note, when you directly copy and paste pip listwill not display the package you copied.

或者,如果它不起作用,您可以直接将paho文件夹从您的Python2.7site-packages文件夹复制并粘贴到您的Python3site-packages文件夹中。我刚才已经证实paho-mqtt 1.2Python2是完全一样paho-mqtt 1.2Python3使用Meld比较工具。请注意,当您直接复制和粘贴pip list时,不会显示您复制的包。

site-packagesare usually inside your system libfolder. It depends upon how Pythonis installed. In my case everything is inside $HOME/.pyenvfolder.

site-packages通常在您的系统lib文件夹中。这取决于如何Python安装。在我的情况下,一切都在$HOME/.pyenv文件夹内。

Remember Python2has it's own site-packagesfolder and Python3has it's own site-packagesfolder where Pythonsearches for the packages. Sometimes if you are using a Debianbased Linux distro please make sure to check inside the dist-packagesfolder as well to see if you can find the package you are looking for.

请记住Python2,它有自己的site-packages文件夹,并Python3有自己的site-packages文件夹用于Python搜索包。有时,如果您使用的是Debian基于 Linux 的发行版,请务必检查dist-packages文件夹内部,看看是否可以找到您正在寻找的软件包。

回答by Fernando Simplicio

You can try install Paho lib:

您可以尝试安装 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