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

Ros python packages __init__ level imports

asked 2018-04-11 22:46:50 -0500

prarobo gravatar image

updated 2018-04-12 08:22:32 -0500

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/...

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-04-13 08:41:39 -0500

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.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2018-04-11 22:46:50 -0500

Seen: 398 times

Last updated: Apr 13 '18