Naff16
Naff16

Reputation: 81

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.

Upvotes: 0

Views: 6827

Answers (3)

Shaukat hussain
Shaukat hussain

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

  • /home/user/anaconda3/lib/python3.7/site-packages/paho

I used this script:

  • sudo cp -r /home/user/anaconda3/lib/python3.7/site-packages/paho /home/user/.local/lib/python3.7/site-packages/

it solved my problem, I hope it helps someone.

Upvotes: 0

Roshan Bagdiya
Roshan Bagdiya

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

Naff16
Naff16

Reputation: 81

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.

Upvotes: 0

Related Questions