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

Adding __package__ to ROS Python scripts breaks import

asked 2015-07-08 09:19:08 -0600

peci1 gravatar image

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?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-20 05:13:16 -0600

peci1 gravatar image

The answer is obvious - relative imports in scripts are not supported. Put all the shared code into a proper Python package (my_pkg/src/my_pkg/module.py) and do from my_pkg import module in the script. Of course, catkin_python_setup() has to be called from CMakeLists.txt and setup.py has to be properly set up.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2015-07-08 09:19:08 -0600

Seen: 251 times

Last updated: May 20 '23