![](/img/trans.png)
[英]SyntaxError when using client = mqtt.Client() in Python 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
pip 安裝 paho-mqtt
在 script.py 目錄中,我運行了以下命令:
這可能不是解決問題的正確方法,但其他方法都不起作用。
可能的原因是
庫“paho”已安裝(默認)在文件夾“/home/user/.local/lib/python2.7/site-packages”中,但“python”在文件夾“/usr/local/”中搜索此庫lib/python2.7/dist-packages”。 可以從這里參考 dist 和 site 包之間的區別。
ln 命令用於在文件之間創建鏈接。因此文件是從 script.py 目錄中引用的。
通過查看 naff 和 Roshan 的回答,就我而言,該軟件包安裝在此位置的 Anaconda 版本的 python 3.7 中
我使用了這個腳本:
它解決了我的問題,我希望它可以幫助某人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.