ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

how to execute python code in ROS

asked 2017-08-22 17:14:54 -0500

kimnguyen gravatar image

updated 2017-08-22 22:38:39 -0500

Ed Venator gravatar image

I created a python file contain the code for the robot patrol around, however, I am not sure how to run the file. I created a package name patrol inside Navigation folder using command:

catkin_create_pkg patrol rospy move_base_msgs.msg
chmod +x
rosrun patrol

The error said: cannot find the package. I am still new to ROS, so I would like to ask how do I compiled and run the code that I wrote and possibly to launch it with the rviz ? Thank you,

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2017-08-23 05:17:19 -0500

Also, don't forget to source !

edit flag offensive delete link more

answered 2017-08-22 22:40:18 -0500

Ed Venator gravatar image

It looks like you never build your package with catkin. This is necessary, even for python nodes, for rosrun to be able to find your code. Look at step 3 in this rospy tutorial.

edit flag offensive delete link more

answered 2017-09-01 14:08:53 -0500

jayess gravatar image

As @skander_hannachi said, you need to source setup.bash of your workspace.

source ~/catkin_ws/devel/setup.bash

You don't need to always run Python nodes using rosrun. If you are in the same directory of the executable you can use


just like any other Python script. rosrun makes it very convenient to run your nodes from any directory (as long as you have your environment variables set properly), but it is not absolutely necessary (for Python).

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-08-22 17:14:54 -0500

Seen: 8,948 times

Last updated: Sep 01 '17