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

Finding Python modules in groovy and with catkin?

asked 2013-01-16 11:44:46 -0500

TommyP gravatar image

Suppose I have a package with two sub directories "src" and "test". Suppose I have a module "a.py" in "src". How do I in a script in "test" import from "a.py"? What is the correct method with regard to finding "a.py" when executing?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2013-01-16 23:06:46 -0500

KruseT gravatar image

updated 2013-01-18 03:47:45 -0500

The recommended method is to use the nosetests library to write tests fpr python. The command is nose. Nose automatically sets the PYTHONPATH before running the tests when you use reasonable names for your folders, like src and test. Doing so, in a python file in the test folder you can simply write "import foo.bar" to import a module defined in src/foo/bar.py.

When you write python modules with catkin, you should also use a setup.py file and the catkin_python_setup macro in your CMakeLists.txt file. More information can be found in the catkin documentation, and rospy tutorials, such as here: http://ros.org/wiki/rospy_tutorials/Tutorials/Makefile and here: http://ros.org/rosdoclite/groovy/api/catkin/html/user_guide/setup_dot_py.html

edit flag offensive delete link more

Comments

I found an example setup.py file and got that to work. But I do not understand exactly what it does. Are there any documentation for setyp.py? Especially I did not get what the "packages" parameter was supposed to be.

TommyP gravatar image TommyP  ( 2013-01-17 00:27:42 -0500 )edit
1

setup.py is the standard build file for python projects. In our case, you declare the scripts and packages from your project, and setup.py makes sure those get installed to the right locations. More info here: http://ros.org/rosdoclite/groovy/api/catkin/html/user_guide/setup_dot_py.html

KruseT gravatar image KruseT  ( 2013-01-18 03:47:29 -0500 )edit

So are you saying that the thing in "packages" in setup.py is not ros packages at all? Are they Python packages or what are they? And if they are ROS packages what is a "relay package" talked about on the web page you gave? (Re-reading this now after reading the SIG discussion about setup.py).

TommyP gravatar image TommyP  ( 2013-02-07 00:32:24 -0500 )edit

yes, 'packages' in the setup.py relate to python packages only. And a relay-package is a folder foo with only an __init__.py file in it that points to the foo folder in the sourcespace with all the python code the user writes.

KruseT gravatar image KruseT  ( 2013-02-07 01:50:19 -0500 )edit

what happens if you accidentally run the setup.py file the tutorial said it can break your ROS installation? Does this mean you have to reinstall ROS if you do this? Is it possible to keep a back up of your ROS workspace and just replace it?

Sentinal_Bias gravatar image Sentinal_Bias  ( 2014-04-02 00:33:55 -0500 )edit

You should ask this as a separate question. The answer is complicated. Nothing will break immediately, but maintenance problems will likely arise later on. Solution: remove all the files setup.py created.

joq gravatar image joq  ( 2014-04-02 03:27:34 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2013-01-16 11:44:46 -0500

Seen: 1,085 times

Last updated: Jan 18 '13