Ask Your Question

Importing ROS Messages in Python

asked 2014-06-30 16:25:35 -0600

nckswt gravatar image

I'm using ROS as middleware to connect my model (in Python) and my view (in JS), so I want to keep my Python code as separate as possible from ROS-related code. My directory structure looks like:

| |
| |
| |
| |
| |
| |____test
| | |____unit
| | | |
| | | |
| |____battery.msg
| |____cmd.msg
| |
| |

I'm trying to import battery.msg from my script, but Python doesn't work too well with relative imports.

I have two questions:

  1. Do I need to add an to my msg folder to be able to import battery.msg? I don't entirely know pythonic standards, but it seems odd that I'd need to designate msg as a module.
  2. Is this an acceptable way to structure my package? I'm trying to be standards-compliant, but I don't know if I am!
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2014-06-30 20:58:53 -0600

nckswt gravatar image

Turns out the problem was with calling myself. The documentation explains issues with calling manually screws things up.

Instead, just uncomment catkin_python_setup() in CMakeLists.txt and follow the instructions in this tutorial.

edit flag offensive delete link more

answered 2014-06-30 17:07:38 -0600

.msg files are not importable by python. Python modules will be generated when you build your package (with catkin or rosbuild), and placed in your package's src/PKG_NAME/msg directory. They will be added to your pythonpath (either automatically with catkin, or with the load_manifest(PKG_NAME) command with rosbuild)

edit flag offensive delete link more


Hmm - it doesn't look like I'm getting cops_and_robots.msg added as a submodule when I examine it using pkgutil even though I think I've configured my CMakeLists and my package.xml properly (see ). rosmsg show cops_and_robots/battery works, though.

nckswt gravatar image nckswt  ( 2014-06-30 20:25:38 -0600 )edit

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


Asked: 2014-06-30 16:25:35 -0600

Seen: 1,014 times

Last updated: Jun 30 '14