[Documentation] [TitleIndex] [WordIndex


There are two parts to initializing your Python code to work in ROS:

  1. Configuring your PYTHONPATH: if you use other ROS Packages in your code, you'll need to dynamically load their libraries onto your path so that you can import them.

  2. Initializing your ROS Node: every node has a name, so your code cannot communicate with other nodes until you have provided this information.

Each of these steps is described in detail below.

Configuring your PYTHONPATH

ROS catkin packages containing python code should use a setup.py file as described here:


This setup.py defines which Python packages and scripts should be installed. Installed packages are added to the PYTHONPATH automatically when you source the devel or install space in which the package was built or installed to.

ROS uses the <depends> listed in your manifest.xml to configure your PYTHONPATH. To use this feature in your code, simple add this to the top of your file.

   1 import roslib; roslib.load_manifest('YOUR_PACKAGE_NAME')

By default, this will add the src and lib directory for each of your dependencies to your PYTHONPATH.

NOTE: If you wish to export a different PYTHONPATH for your package, you can add a <python> tag to the <export> section of your manifest.xml. For example:

 <python path="${prefix}/different_dir"/>
 .. other exports

Initializing your ROS Node

See also: rospy.init_node() Code API

One of the first calls you will likely execute in a rospy program is the call to rospy.init_node(), which initializes the ROS node for the process. You can only have one node in a rospy process, so you can only call rospy.init_node() once.

The two most common invocations for init_node() are:



rospy.init_node('my_node_name', anonymous=True)

As part of the init_node() call, you will pass in the default name of your node. When you run your code, this is the name that your node will appear as online unless it's overridden by remapping arguments. Names have important properties in ROS. Most importantly, they must be unique. In cases where you don't care about unique names for a particular node, you may wish to initialize the node with an anonymous name.

rospy.init_node(name, anonymous=False, log_level=rospy.INFO, disable_signals=False)

Accessing your command-line arguments

ROS passes in additional command-line arguments to your program to initialize remapping arguments. While these are easy to filter out on your own, rospy provides a convenience function for stripping these out.


Shutting down

Testing for shutdown

The most common usage patterns for testing for shutdown in rospy are:

while not rospy.is_shutdown():
   do some work


... setup callbacks

The spin() code simply sleeps until the is_shutdown() flag is True. It is mainly used to prevent your Python Main thread from exiting.

There are multiple ways in which a node can receive a shutdown request, so it is important that you use one of the two methods above for ensuring your program terminates properly.

Registering shutdown hooks


You can request a callback using rospy.on_shutdown() when your node is about to begin shutdown. This will be invoked before actual shutdown occurs, so you can perform service and parameter server calls safely. Messages are not guaranteed to be published.

def myhook():
  print "shutdown time!"


Manual shutdown (Advanced)

If you are overriding rospy's signal handling (the disable_signals option to init_node()), you will need to manually invoke the correct shutdown routines to cleanup properly.


2019-04-20 13:13