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

How can I use custom python functions in other ROS packages?

asked 2011-11-03 20:52:05 -0600

Sabrina gravatar image

updated 2014-01-28 17:10:42 -0600

ngrennan gravatar image

Hi everybody,

I'm quite new to python programming in ROS (mainly used roscpp before). I generated a ROS package (let's call it "useful_python_functions") not providing any ROS node but several python functions. I wrote a python file (let's call it "") and saved it in useful_python_functions/src/

Now I want to use the functions in other ROS packages. I added useful_python_functions as a dependency to the manifest.xml and then tried in my ROS node:

from useful_python_functions import utils

Running the node I get:

ImportError: cannot import name utils

As I saw here the src folder of useful_python_functions should be part of my PYTHONPATH by default.

How can I import the functions of in a ROS node from a different ROS package?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2011-11-03 21:52:01 -0600

updated 2011-11-03 23:54:31 -0600

By default stating a dependency in manifest.xml adds the following path to your module:


You can check this by doing:

import roslib
import sys
print sys.path

This convention is used when creating custom messages, as their implementation will go into a folder named after the package.

Therefore, in your package useful_python_functions, create a folder named again "useful_python_functions" in the src folder. You need an file in the folder (even if it's empty) so that python knows that folder is a root module. Then the file will be available to any package that depends on useful_python_functions.

Hope it helps, and welcome to the wonderful python language!

edit flag offensive delete link more


Thank you, Lorenzo. This works with a small addition: I had to add an empty to src/useful_python_functions. (Except when I run rosbuild_genmsg() in CMakeLists.txt. Then is generated automatically.)
Sabrina gravatar image Sabrina  ( 2011-11-03 22:41:05 -0600 )edit
You're right, I forgot that! I am adding editing the answer. Thanks!
Lorenzo Riano gravatar image Lorenzo Riano  ( 2011-11-03 23:53:06 -0600 )edit

can this solution be used on groovy? cause i have tried it, but it showed "terminate called after throwing an instance of 'rospack::Exception' what(): error parsing manifest of package state_boost at /home/chao/catkin_ws/src/state_boost/package.xml"

chao gravatar image chao  ( 2014-01-21 00:51:02 -0600 )edit

I can get this working on catkin/groovy either. I added a run depend in the package.xml and did a catkin_make and it still can find the module. edit For groovy I think you need to use a file...

Sentinal_Bias gravatar image Sentinal_Bias  ( 2014-04-01 23:47:43 -0600 )edit

answered 2017-08-18 04:19:21 -0600

Will Chamberlain gravatar image

updated 2017-12-20 14:13:09 -0600

jayess gravatar image

For other noobs like me: I was having trouble even using a class defined in another module of the _same_ package. This is what worked;



  • is empty
  • is the module; it contains a class called ConfigReader
  • uses an instance of class ConfigReader contains

an_instance = ConfigLoader('~/config.txt')
edit flag offensive delete link more

answered 2014-02-16 16:20:47 -0600

Troy gravatar image

If you are trying to import a python module for a different package you need 2 things (lets assume the file you want to import is in a package called utils).

1) On the manifest.xml of the package where you are trying to import to, you need to include a <depend package="utils"/> 2) The python module that you want to import must be in folder /src within the utils package (not /scripts), as the /src folder is automatically exported to PYTHONPATH.

This second step took me quite a while to figure out. It will not work if your python files are in a folder other than src. If you do want to have the python files in a folder other than /src then you need to add an <export> command to the manifest.xml in the utils package.

You can of course just add an export PYTHONPATH command to your .bashrc file, but that makes things messy of you are building code on one machine then migrating it to another (ie your robot).

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2011-11-03 20:52:05 -0600

Seen: 5,598 times

Last updated: Dec 20 '17