Reputation: 81
I'm trying to use paho-mqtt in a python project, im using pycharm as my IDE. I installed paho-mqtt using: pip install paho-mqtt, but it seems that something is not right. Because when i deploy the following script:
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()
is giving me the following error:
/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
And paho-mqtt is appearing me part of the installed packages.
Did someone already had this issue and got it solved?
Thanks.
Upvotes: 0
Views: 6827
Reputation: 55
By looking at naff's and Roshan's answer, In my case, the package was installed in Anaconda version of python 3.7 at this location
I used this script:
it solved my problem, I hope it helps someone.
Upvotes: 0
Reputation: 2178
probably reason for this is
the library "paho" has been installed (for default) in the folder "/home/user/.local/lib/python2.7/site-packages" but "python" search this library in the folder "/usr/local/lib/python2.7/dist-packages". difference between dist and site packages can be referred from here.
The ln command is used to create links between files.thus the file is getting referred from script.py directory.
Upvotes: 0
Reputation: 81
I solved the issue the using the following issue as an example: https://github.com/shivasiddharth/GassistPi/issues/725
pip install paho-mqtt
In the script.py directory i ran the following commands:
This may not be the correct way to solve the issue, but nothing else was working.
Upvotes: 0