Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

You python script runs fine for me from cmdline. I suspect you have a path issue with pycharm. This link may help stackoverflow.com/questions/19885821/… – al76 May 10, 2019 at 0:40 I need check the path in the pycharm. But yesterday i tried to run the script from the cmdline as: python script.py and it gave me the same error as pycharm. – Naff16 May 10, 2019 at 11:29

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

  • Installed paho-mqtt using:
  • pip install paho-mqtt

  • 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.

    probably reason for this is

    the library "paho" has been installed (for default) in the folder "/home/user/.local/lib/python2.7/site-packages" "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.

    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.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.