ROS Package Library
rospkg is a standalone Python library for the ROS (Robot Operating System) package system. It provides utilities for querying information about ROS packages, stacks, and distributions, abstracting the underlying filesystem layout. The current version is 1.6.1, with releases occurring every few months to once a year, reflecting active but measured development.
Warnings
- breaking rospkg is not API compatible with older `roslib` modules it replaced (e.g., `roslib.manifest`, `roslib.packages`). Direct migration from `roslib` APIs will require code changes.
- gotcha The `rospkg.ResourceNotFound` exception is commonly encountered if the ROS environment is not properly sourced (e.g., `setup.bash` not run) or the requested package does not exist within the active ROS workspace or installed distribution.
- gotcha While `pip install rospkg` is standard, in a full ROS installation, `rospkg` is often installed as a system dependency via `apt` (e.g., `sudo apt-get install python3-rospkg`). Mixing `pip` and `apt` installations of `rospkg` or its dependencies can lead to conflicts and unexpected behavior, especially in complex ROS environments.
Install
-
pip install rospkg
Imports
- RosPack
from rospkg import RosPack
- ResourceNotFound
from rospkg.common import ResourceNotFound
Quickstart
import rospkg
import os
try:
# Initialize RosPack to query ROS packages
r = rospkg.RosPack()
# Get the path to a package, e.g., 'rospy'
rospy_path = r.get_path('rospy')
print(f"Path to rospy package: {rospy_path}")
# List direct dependencies of a package, e.g., 'roscpp'
# Note: 'roscpp' is typically a C++ package, but rospkg can still find its dependencies
try:
roscpp_depends = r.get_depends('roscpp')
print(f"Dependencies of roscpp: {roscpp_depends}")
except rospkg.ResourceNotFound:
print("roscpp package not found, cannot list dependencies. Is a ROS environment sourced?")
# Check if a package exists
if r.has_package('rospy'):
print("rospy package exists.")
except rospkg.ResourceNotFound as e:
print(f"Error: ROS resource not found: {e}. Ensure ROS environment is sourced correctly.")
except Exception as e:
print(f"An unexpected error occurred: {e}")