Search code examples
pythonpython-3.xros

@!/usr/bin/env: No such file or directory when i try to use ros nodes


I tried to write a subscriber and publisher node; but when I try to run the publisher with rosrun, it gives me an error: @!/usr/bin/env: No such file or directory

Here is the error and the corresponding code (I did chmod +x):

Publisher node:

@!/usr/bin/env python

import rospy
from std_msgs.msg import String

def publish_it():
    pub = rospy.Publisher("first_msgs",String , queue_size = 10)
    rospy.init_node("publisher_node",anonymous = True)
    x = 1
    z = 10
    rate = rospy.Rate(z) #1/z sec delay

    while not rospy.is_shutdown():
        x += 1
        if x == 100:
            x = 0

        msg = "Hi this is our first message . times:" + str(x)
        rospy.loginfo(msg)
        pub.publish(msg)
        rate.sleep()

if __name__ == "__main__":
    try:
        publish_it()
    except rospy.ROSInterruptException:
        pass

Subscriber node:

@!/usr/bin/env python

import rospy
from std_msgs.msg import String

def callback(data):
    rospy.loginfo(data.data)

def iSeeIt():
    rospy.init_node("subscriber_node",anonymous = True)
    rospy.Subscriber("first_msgs",String,callback)
    rospy.spin()

if __name__ == "__main__":
    iSeeIt()

Solution

  • I solved it by changing

    @!/usr/bin/env python
    

    with

    #!/usr/bin/python2.7
    

    I am using python3 and I don't really understand how it works but it works.