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

2 Answers

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 imagegvdhoorn ( 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 imagenbro ( 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 imagenbro ( 2018-04-08 08:40:15 -0500 )edit
-1

answered 2018-04-07 21:05:48 -0500

elvenRobot gravatar image

The rospack read and parse the xml for each package, and assemble a complete dependency tree for all packages. So, you have to add <depend>turtlesim</depend> in package.xml. Now "rospack depends" will show you Turtlesim as a dependency.

edit flag offensive delete link more

Comments

This does not answer my question(s) at all.

nbro gravatar imagenbro ( 2018-04-08 06:37:51 -0500 )edit
1

@nbro: can you please not mark answers that you don't like as offensive (or at least, I assume that was you). That is not what that button is for. If you disagree (strongly) with an answer, just downvote it.

gvdhoorn gravatar imagegvdhoorn ( 2018-04-08 11:04:49 -0500 )edit

@gvdhoorn Yes, it's me. I would downvote if I had enough reputation.

nbro gravatar imagenbro ( 2018-04-08 11:09:48 -0500 )edit
1

If you'd accept some more of the answers to some of your questions, or answer some questions yourself, you'd have the rep.

gvdhoorn gravatar imagegvdhoorn ( 2018-04-08 11:12:37 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

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

Seen: 126 times

Last updated: Apr 08 '18