Module has no attribute 'Subscribe'

Hi, I’m attempting the quiz now and am stuck at reading the laser values.

#! /usr/bin/env python

import rospy
from std_msgs.msg import Int32
from sensor_msgs.msg import LaserScan

def callback2(msg):
print msg.ranges

rospy.init_node(‘topic_subscriber’)
sub = rospy.Subscribe(‘/kobuki/laser/scan’, LaserScan, callback2)
rospy.spin() #! /usr/bin/env python

and the error is:

user:~/catkin_ws/src/my_examples_pkg/scripts$ python robotMani.py
Traceback (most recent call last):
File “robotMani.py”, line 11, in
sub = rospy.Subscribe(‘/kobuki/laser/scan’, LaserScan, callback2)
AttributeError: ‘module’ object has no attribute ‘Subscribe’
user:~/catkin_ws/src/my_examples_pkg/scripts$

What’s wrong here?
Is ‘module’ the intepreter from robotignite?
Thanks heaps.

Hi @zaki.mohzani,
There is a small typo in your code which is causing the problem try debugging it on your own, if you still can’t figure it out press on the arrow below for the solution.

Solution

There is no rospy.Subscribe instead it should be sub = rospy.Subscriber('/kobuki/laser/scan', LaserScan, callback2).

Other than that small typo your code seems to work fine.

Happy Learning…!! :innocent:

2 Likes

Dang, thanks man. Why didn’t the error log mention something like:
“rospy” object has no attribute Subscribe OR
“sub” object has no attribute Subscribe OR
“LaserScan” object has no attribute Subscribe

Why did it mention this “module”? I kept thinking that the LaserScan class was missing this method “Subscribe” method.

Hi @zaki.mohzani,
That’s a very interesting question and infact I have no answer for it, I have done some digging and
I’m not really sure but if I were to take a guess I’d say its something to do with how rospy works.

If you look at the source code of rospy, It actually doesn’t contain any classes of its own, it imports everything from other modules.

Maybe someone more experienced from the community could weigh in and help us out.

module is referring here to rospy, as hinted in the error traceback:

sub = rospy.Subscribe(’/kobuki/laser/scan’, LaserScan, callback2)
AttributeError: ‘module’ object has no attribute ‘Subscribe

You were trying to access a Subscribeattribute of the rospy module, but that attribute does not exist.

Thanks bayodesegun. Why didn’t it say the following instead?

AttributeError: ‘rospy’ object has no attribute ‘Subscribe

This would have been less confusing.

That would be a question for the Python team :slightly_smiling_face:. My guess is that it’s probably too much hassle for them to specify the name of the affected module, especially that it can be deduced from the traceback.

If the object was an instance of a class, the class name is usually mentioned. But in this case, the “object” was a module.

1 Like