Bat64
Posts: 3
Joined: Fri Nov 17, 2017 9:39 am

Paho-MQTT: NameError: name 'Client" is not defined

Fri Nov 17, 2017 9:49 am

Hello,

I am running a short python test script to publish to an MQTT topic, my code is as follows:

Code: Select all

import paho.mqtt.client as client

Client(client_id="Pi")
client=mqtt.Client("Sensors")
username_pw_set("censored", password="censored")
client.connect("hostname")
client.publish("/temp", "369")
I receive the following error:

Traceback (most recent call last):
File "mqtttest.py", line 3, in <module>
Client(client_id="Pi")
NameError: name 'Client' is not defined

B.Goode
Posts: 4689
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Paho-MQTT: NameError: name 'Client" is not defined

Fri Nov 17, 2017 10:10 am

Welcome to the Raspberry Pi forums.

The opinion of the Python interpreter is always correct, even when you think it is wrong.

Do you have a reference to any documentation or tutorial that might be misleading you into thinking that your script is correct?

Bat64
Posts: 3
Joined: Fri Nov 17, 2017 9:39 am

Re: Paho-MQTT: NameError: name 'Client" is not defined

Fri Nov 17, 2017 11:03 am

Thank you for your response. I understand that, I did not mean to imply that it is wrong.

Here are my sources:

http://www.steves-internet-guide.com/in ... on-client/
https://pypi.python.org/pypi/paho-mqtt/

I suspect it may be an incorrect installation or my script is in the wrong directory since it seems like it is not picking up the method "Client"

I am using python3 so I installed it with

Code: Select all

pip3 install paho-mqtt
It is in the directory /etc/openhab2/scripts
However, there are other scripts in there that returned a "connection refused" error which I believe may nullify my above theory.

User avatar
DougieLawson
Posts: 30430
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Paho-MQTT: NameError: name 'Client" is not defined

Fri Nov 17, 2017 11:07 am

client <> Client

Python is case sensitive.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

Bat64
Posts: 3
Joined: Fri Nov 17, 2017 9:39 am

Re: Paho-MQTT: NameError: name 'Client" is not defined

Fri Nov 17, 2017 11:23 am

Well I guess I'm not copy-pasting from Steve's internet guide anymore... Thank you so much! Anyway I now have the following error:

Code: Select all

Traceback (most recent call last):
  File "mqtttest.py", line 3, in <module>
    client(client_id="Pi")
TypeError: 'module' object is not callable

User avatar
DougieLawson
Posts: 30430
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Paho-MQTT: NameError: name 'Client" is not defined

Sat Nov 18, 2017 1:27 am

Bat64 wrote:
Fri Nov 17, 2017 11:23 am
Well I guess I'm not copy-pasting from Steve's internet guide anymore... Thank you so much! Anyway I now have
You didn't read the whole article.

Code: Select all

import paho.mqtt.client as mqtt #import the client1
broker_address="localhost" 
#broker_address="iot.eclipse.org" #use external broker
client = mqtt.Client("P1") #create new instance
client.connect(broker_address) #connect to broker
client.publish("house/main-light","OFF")#publish
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 20 guests