Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Adding __package__ to ROS Python scripts breaks import

I wanted to do a relative import from my Python ROS node.

So I added a __package__ = 'my_package' specification to the beginning of the file (right under the shebang). This is needed, because for a script, __package__ == "__main__", instead of the real package name.

Since then, I started receiving warnings when running the node using rosrun:

Parent module 'my_package' not found while handling absolute import from sensor_msgs.msg import PointCloud2

And, also, there was an error reported for the relative import:

Parent module 'my_package' not loaded, cannot perform relative import

It seems like the __package__ definition somehow messes up all other ROS imports.

So I tried putting this definition after all absolute imports, and right before the relative import. The same result, though.

Putting it after the relative import is obviously useless, because it is added to allow me to use relative imports.

The only way to get it working was to give up using relative import and use the normal absolute ROS package paths.

So the question is: Are relative imports supported in ROS scripts at all? If yes, how?