Robotics StackExchange | Archived questions

subscriber node showing error while try running

whenever I want to run the following code in http://wiki.ros.org/rospy_tutorials/Tutorials/WritingPublisherSubscriber#CA-1b01f1a47bfa1c005374252f8e11db76472de33e_5, that is:

#!/usr/bin/env python
import rospy

from std_msgs.msg import String

def callback(data):

    rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)

def listener():

    # In ROS, nodes are uniquely named. If two nodes with the same
    # node are launched, the previous one is kicked off. The
    # anonymous=True flag means that rospy will choose a unique
    # name for our 'talker' node so that multiple talkers can
    # run simultaneously.
    rospy.init_node('listener', anonymous=True)
    rospy.Subscriber("chatter", String, callback)
    # spin() simply keeps python from exiting until this node
    #is stopped
    rospy.spin()
if __name__ == '__main__':
    listener()

I get the following error at terminal:

from: can't read /var/mail/std_msgs.msg
/home/labid52/catkin_ws/src/hello_world/script/listener.py: line 3: syntax error near unexpected token `('
/home/labid52/catkin_ws/src/hello_world/script/listener.py: line 3: `def callback(data):'

Asked by Labid52 on 2018-10-24 03:32:13 UTC

Comments

Answers

It is very likely you have a space or an empty line before the #!/usr/bin/env python line in your script.

Asked by gvdhoorn on 2018-10-24 03:35:57 UTC

Comments