Ask Your Question
6

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

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

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

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 "utils.py") and saved it in useful_python_functions/src/utils.py.

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 utils.py in a ROS node from a different ROS package?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
9

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

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

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

<module_you_depend_on>/src/<module_you_depend_on>

You can check this by doing:

import roslib
roslib.load_manifest("your_package")
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 __init__.py file in the folder (even if it's empty) so that python knows that folder is a root module. Then the file utils.py 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

Comments

1
Thank you, Lorenzo. This works with a small addition: I had to add an empty __init__.py to src/useful_python_functions. (Except when I run rosbuild_genmsg() in CMakeLists.txt. Then __init__.py is generated automatically.)
Sabrina gravatar imageSabrina ( 2011-11-03 22:41:05 -0500 )edit
You're right, I forgot that! I am adding editing the answer. Thanks!
Lorenzo Riano gravatar imageLorenzo Riano ( 2011-11-03 23:53:06 -0500 )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 imagechao ( 2014-01-21 00:51:02 -0500 )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 setup.py file...

Sentinal_Bias gravatar imageSentinal_Bias ( 2014-04-01 23:47:43 -0500 )edit
0

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

Will Chamberlain gravatar image

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

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;

[catkin_ws_path]/src/my_package/src/my_package/\__init__.py

[catkin_ws_path]/src/my_package/src/my_package/file_checker.py 

[catkin_ws_path]/src/my_package/src/my_package/script_im_trying_to_run.py
  • __init__.py is empty
  • file_checker.py is the module; it contains a class called ConfigReader
  • script_im_trying_to_run.py uses an instance of class ConfigReader

script_im_trying_to_run.py contains

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

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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

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

Seen: 4,324 times

Last updated: Dec 20 '17