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

How do I test the ROS version in Python code?

asked 2012-06-13 16:34:50 -0600

StFS gravatar image

updated 2014-01-28 17:12:40 -0600

ngrennan gravatar image

This has been asked for C++ ( but it would be good to know what the correct way to do this is in Python.

There is a python script called rosversion which does this in a bit of an awkward way IMO. It does it by checking whether the "ROS_DISTRO" environment variable exists and, if not, it parses an XML file (roscore.xml). This seems quite complicated and it only gives you the release name (cturtle, electric, fuerte, etc.).

Is there a way to get access to those nice looking DEFINES like in the C++ code? Even having just the functionality of the rosversion script somewhere in an official ROS Python API function would be better than nothing (just getting the release name).

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2012-06-14 00:59:33 -0600

joq gravatar image

updated 2012-06-14 01:08:17 -0600

If you look further down in rosversion, it only prints the distro name when invoked with the -d or --distro option. To get the ROS version string, invoke:

$ rosversion ros

On Fuerte, that returns 1.8.7 (without a newline), on Electric 1.6.9 (with a newline).

From a Python script, you can do that yourself:

import rospkg
version = rospkg.RosStack().get_stack_version('ros')

That uses the new distro-independent rospkg module. Get it like this:

sudo pip install -U rospkg

It works for other stacks, as well:

messages_version = rospkg.RosStack().get_stack_version('common_msgs')

In most cases, it is better to test for the existence of some new API instead:

TimeReference = None
    from sensor_msgs.msg import TimeReference
except ImportError:

Then, make your logic dependent on the existence of that message:

if TimeReference:
    tr_pub = rospy.Publisher('gps_time', TimeReference)
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2012-06-13 16:34:50 -0600

Seen: 2,174 times

Last updated: Jun 14 '12