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
Import Error: paho.mqtt.client not found
提问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 Python3
but for some reason the interpreter defaults to version 2.7 in Linux.
您已经安装,Python3
但由于某种原因,解释器在 Linux 中默认为 2.7 版。
Try using pip3 install paho-mqtt python-etcd
instead.
尝试使用pip3 install paho-mqtt python-etcd
。
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.
或者,如果它不起作用,您可以直接将paho
文件夹从您的Python2.7
site-packages
文件夹复制并粘贴到您的Python3
site-packages
文件夹中。我刚才已经证实paho-mqtt 1.2
为Python2
是完全一样paho-mqtt 1.2
的Python3
使用Meld
比较工具。请注意,当您直接复制和粘贴pip list
时,不会显示您复制的包。
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.
site-packages
通常在您的系统lib
文件夹中。这取决于如何Python
安装。在我的情况下,一切都在$HOME/.pyenv
文件夹内。
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.
请记住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