0
$\begingroup$

Rosanswers logo

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?


Originally posted by Sabrina on ROS Answers with karma: 285 on 2011-11-03

Post score: 6

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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!


Originally posted by Lorenzo Riano with karma: 1342 on 2011-11-03

This answer was ACCEPTED on the original site

Post score: 9


Original comments

Comment by Lorenzo Riano on 2011-11-03:
You're right, I forgot that! I am adding editing the answer. Thanks!

Comment by Sabrina on 2011-11-03:
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.)

Comment by chao on 2014-01-21:
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"

Comment by Sentinal_Bias on 2014-04-01:
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...

$\endgroup$
0
$\begingroup$

Rosanswers logo

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')
....

Originally posted by Will Chamberlain with karma: 56 on 2017-08-18

This answer was NOT ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.