ImportError: No module named mqtt.client Error [paho-mqtt]
ImportError: No module named mqtt.client Error [paho-mqtt]
我正在尝试在 python 项目中使用 paho-mqtt,我使用 pycharm 作为我的 IDE。
我安装了 paho-mqtt 使用:pip install paho-mqtt,但似乎有些地方不对。因为当我部署以下脚本时:
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("/test")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
给我以下错误:
/usr/bin/python2.7 /home/user/PycharmProjects/untitled/MQTT/paho.py
Traceback (most recent call last):
File "/home/user/PycharmProjects/untitled/MQTT/paho.py", line 1, in <module>
import paho.mqtt.client as mqtt
File "/home/user/PycharmProjects/untitled/MQTT/paho.py", line 1, in <module>
import paho.mqtt.client as mqtt
ImportError: No module named mqtt.client
Process finished with exit code 1
paho-mqtt 正在向我显示已安装软件包的一部分。
有人遇到过这个问题并解决了吗?
谢谢。
我使用以下问题作为示例解决了这个问题:https://github.com/shivasiddharth/GassistPi/issues/725
- 安装 paho-mqtt 使用:
pip install paho-mqtt
在script.py目录下我运行执行以下命令:
- ln -s /home/user/.local/lib/python2.7/site-packages/paho 帕霍
- ln -s /home/user/.local/lib/python2.7/site-packages/paho_mqtt-1.4.0.dist-info paho_mqtt-1.4.0.dist-info
这可能不是解决问题的正确方法,但其他方法均无效。
可能是因为
库 "paho" 已安装(默认)在文件夹“/home/user/.local/lib/python2.7/site-packages”
但
"python" 在文件夹“/usr/local/lib/python2.7/dist-packages”中搜索此库。
dist 和 site 包的区别可以参考 here.
ln 命令用于在 files.thus 从 script.py 目录引用的文件之间创建链接。
通过查看 naff 和 Roshan 的回答,在我的例子中,该软件包安装在 python 3.7 的 Anaconda 版本中的这个位置
- /home/user/anaconda3/lib/python3.7/site-packages/paho
我使用了这个脚本:
- sudo cp -r /home/user/anaconda3/lib/python3.7/site-packages/paho
/home/user/.local/lib/python3.7/站点包/
解决了我的问题,希望对大家有帮助。
我正在尝试在 python 项目中使用 paho-mqtt,我使用 pycharm 作为我的 IDE。 我安装了 paho-mqtt 使用:pip install paho-mqtt,但似乎有些地方不对。因为当我部署以下脚本时:
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("/test")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
给我以下错误:
/usr/bin/python2.7 /home/user/PycharmProjects/untitled/MQTT/paho.py
Traceback (most recent call last):
File "/home/user/PycharmProjects/untitled/MQTT/paho.py", line 1, in <module>
import paho.mqtt.client as mqtt
File "/home/user/PycharmProjects/untitled/MQTT/paho.py", line 1, in <module>
import paho.mqtt.client as mqtt
ImportError: No module named mqtt.client
Process finished with exit code 1
paho-mqtt 正在向我显示已安装软件包的一部分。
有人遇到过这个问题并解决了吗?
谢谢。
我使用以下问题作为示例解决了这个问题:https://github.com/shivasiddharth/GassistPi/issues/725
- 安装 paho-mqtt 使用:
pip install paho-mqtt
在script.py目录下我运行执行以下命令:
- ln -s /home/user/.local/lib/python2.7/site-packages/paho 帕霍
- ln -s /home/user/.local/lib/python2.7/site-packages/paho_mqtt-1.4.0.dist-info paho_mqtt-1.4.0.dist-info
这可能不是解决问题的正确方法,但其他方法均无效。
可能是因为
库 "paho" 已安装(默认)在文件夹“/home/user/.local/lib/python2.7/site-packages” 但 "python" 在文件夹“/usr/local/lib/python2.7/dist-packages”中搜索此库。 dist 和 site 包的区别可以参考 here.
ln 命令用于在 files.thus 从 script.py 目录引用的文件之间创建链接。
通过查看 naff 和 Roshan 的回答,在我的例子中,该软件包安装在 python 3.7 的 Anaconda 版本中的这个位置
- /home/user/anaconda3/lib/python3.7/site-packages/paho
我使用了这个脚本:
- sudo cp -r /home/user/anaconda3/lib/python3.7/site-packages/paho /home/user/.local/lib/python3.7/站点包/
解决了我的问题,希望对大家有帮助。