0
$\begingroup$

Rosanswers logo

I noticed that in native ROS packages all available modules seem to be imported at package level always. For example let us consider the rosbag python package. This is the output I see when I print the list of modules imported at package-level.

>>> import rosbag
>>> dir(rosbag)
['Bag', 'Compression', 'ROSBagException', 'ROSBagFormatException', 'ROSBagUnindexedException', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'bag', 'migration', 'rosbag_main', 'rosbagmain']

However the __init__.py file of rosbag package does not explicitly import bag, migration and rosbag_main. How are these these modules getting imported by themselves?

In order to verify if this is something done by the catkin build system, I wrote my own ros python package. My custom package did't any modules imported at package level. So my question is what makes the ROS native python packages special to cause this behavior?

In order to understand this question better, if you want to access the rosbag source, see the link https://github.com/ros/ros_comm/tree/lunar-devel/tools/rosbag/src/rosbag


Originally posted by prarobo on ROS Answers with karma: 35 on 2018-04-11

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

This is not a ROS-specific issue, and is instead expected Python behavior when using relative module imports within a package. I just looked through a bunch of Python documentation trying to figure out somewhere where it justifies or explains this behavior, and I really couldn't find much. I might have missed something, but I found no reference to the behavior you are seeing in PEP 328, official Python 2.7 Modules documentation, or this awesome SO answer on Python modules, packages, and scripts. Eventually, I did find a few references that at least mention this behavior:

It seems to me that whatever package test you used, didn't properly mimic the structure of the rosbag package.


Originally posted by jarvisschultz with karma: 9031 on 2018-04-13

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.