Search code examples
pythonpycharmpaho

ImportError: No module named mqtt.client Error [paho-mqtt]


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.


Solution

  • I solved the issue the using the following issue as an example: https://github.com/shivasiddharth/GassistPi/issues/725

    1. Installed paho-mqtt using:

    pip install paho-mqtt

    1. In the script.py directory i ran the following commands:

      • ln -s /home/user/.local/lib/python2.7/site-packages/paho 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

    This may not be the correct way to solve the issue, but nothing else was working.