0
$\begingroup$

Rosanswers logo

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?


Originally posted by peci1 on ROS Answers with karma: 1366 on 2015-07-08

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by peci1 with karma: 1366 on 2023-05-20

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.