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

Why can I import from a package which is not part of my dependencies?

asked 2018-04-07 19:22:48 -0500

nbro gravatar image

I created a ROS package, my_package, with dependencies rospy and geometry_msgs. Then I created a Python script, my_script.py, (which is located under src) for my_package.

To my surprise, in my_script.py, I can import from the package turtlesim, like from turtlesim.msg import Pose, without getting errors. So, turtlesim seems to be part of my dependencies. Why is that?

If execute the following command on the terminal rospack depends my_package, the output is the following:

cpp_common
rostime
roscpp_traits
roscpp_serialization
catkin
genmsg
genpy
message_runtime
std_msgs
geometry_msgs
gencpp
geneus
gennodejs
genlisp
message_generation
rosbuild
rosconsole
rosgraph_msgs
xmlrpcpp
roscpp
rosgraph
ros_environment
rospack
roslib
rospy

which does not include turtlesim.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-04-08 02:20:05 -0500

gvdhoorn gravatar image

updated 2018-04-08 11:03:56 -0500

To my surprise, in my_script.py, I can import from the package turtlesim, like from turtlesim.msg import Pose, without getting errors. So, turtlesim seems to be part of my dependencies. Why is that?

turtlesim is not part of your dependencies, as it isn't in the list that rospack depends shows, is not listed in your package.xml nor your CMakeLists.txt. Questions #q217475 and #q215059 can perhaps provide some more insight into how package dependencies are encoded and resolved by the various pieces of infrastructure.

As to why you can import the turtlesim Python module, that is because it is on the PYTHONPATH as soon as you source the appropriate setup.(ba|z)sh. If it's installed from a deb, you can find all ROS packages that provide Python modules under /opt/ros/$distro/lib/python2.7/dist-packages. As Python does not need any linking or compilation, just placing a module on its search path is enough to be able to import it.


Edit:

When I said " seems to be part of my dependencies" I didn't mean that it's part of my dependencies, in fact, I said, "seems to".

And I just made it explicit that it isn't.

Do you ever assume that a package is under /opt/ros/$distro/lib/python2.7/dist-packages or do you always prefer to specify the dependency in the package.xml file, even if you have the same package under /opt/ros/$distro/lib/python2.7/dist-packages?

Never assume anything. You cannot assume anything about the state of the system that your package / script(s) will be run on: your next user might have a full install of "all" ROS pkgs, or have a very barren installation with just ros_comm and some dependencies. Always make your dependencies explicit so that your users know what they should provide (but then use rosdep to take care of that for you, #q252478).

edit flag offensive delete link more

Comments

Note that the fact that you imported turtlesim in one of your scripts now does make it a runtime dependency of my_package, but one which is implicit, as you've not expressed that dependency in any of the files that normally list those (ie: package.xml).

gvdhoorn gravatar image gvdhoorn  ( 2018-04-08 02:21:23 -0500 )edit

When I said " seems to be part of my dependencies" I didn't mean that it's part of my dependencies, in fact, I said, "seems to".

nbro gravatar image nbro  ( 2018-04-08 08:37:01 -0500 )edit

Do you ever assume that a package is under /opt/ros/$distro/lib/python2.7/dist-packages or do you always prefer to specify the dependency in the package.xml file, even if you have the same package under /opt/ros/$distro/lib/python2.7/dist-packages?

nbro gravatar image nbro  ( 2018-04-08 08:40:15 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-07 19:22:48 -0500

Seen: 406 times

Last updated: Apr 08 '18