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

ros2 external

asked 2018-06-12 14:19:05 -0600

Faraz Khan gravatar image

updated 2018-06-12 19:45:17 -0600

jayess gravatar image

Hello, What the best way to get the package/file path of a external library thats not part of my ROS2 project directly without having to hardcode the path in a launch file or node?

I have a node - say 'node_py' which requires a particular data file that part of another git repo that ive added as a submodule to my project. So my directory structure looks like:



I've tried making 'external' a package by adding a package.xml but I can't figure out a non-hacky way to get a relative path to that txt file.

Any pointers would be appreciated - im okay with changing the structure a bit if it makes ros2 conventions work. Thanks!

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2018-06-12 19:17:20 -0600

William gravatar image

You would need to install that file somehow (maybe using package_data in the , see: ), or using the install() macro if you're using CMake.

Since this is Python, you can try to access these files at runtime using:

Or if you installed them and want to use ROS to find the files at runtime, you can use the "ament_index" to locate the package's installation directory and find the file relatively from there, see:

edit flag offensive delete link more


Thanks @William! I tried the ament_index method already but this ends up giving me the 'install' prefix which is not where the actual files get copied over. Another simple alternative: Is there a way to get the BASE path of my project within the ros framework? So the main src directory or src/..

Faraz Khan gravatar image Faraz Khan  ( 2018-06-12 20:20:03 -0600 )edit

You cannot assume the source folder of your project is available at runtime. This is a pretty significant difference from ROS 1 where catkin provided the source space when using the devel mode. We don't have that (on purpose) in ROS 2. You need to install the file and find there.

William gravatar image William  ( 2018-06-12 20:23:31 -0600 )edit

If you're running a temporary script from the source folder then you can use the __file__ in your python script to get the location of it in the src folder and then navigate relatively from there. But that won't work for install or release of your package.

William gravatar image William  ( 2018-06-12 20:24:59 -0600 )edit

Gotcha - so I guess the idea is that any package in ROS2 would HAVE to be a proper ROS package (with package.xml/ I want to refer to it and get installed etc? No support for external non-ros libraries to be referenced by other ROS packages? Or do i misunderstand? Thanks!

Faraz Khan gravatar image Faraz Khan  ( 2018-06-12 20:36:03 -0600 )edit

I suppose that’s correct, though not all of the terminology you’re using is familiar to me. Having a package.xml doesn’t do anything magic by itself. There are some ways that might help you but we’re not searching your computer for package.xml files at runtime or anything like that.

William gravatar image William  ( 2018-06-12 21:47:06 -0600 )edit

Gotcha. Lastly: using package_data or include_package_data im unable to get the files to show up in the install directory - Is there something special I have to do for that to happen? Thanks and sorry for the back and forth!

Faraz Khan gravatar image Faraz Khan  ( 2018-06-12 22:01:02 -0600 )edit

Here's an example of a package that uses package_data to install files: We didn't have to do anything else in particular for that case.

dhood gravatar image dhood  ( 2018-06-12 22:10:04 -0600 )edit

Oh man thank you its finally done! it install by default in install/lib/python3.6/site-packages. I changed that using setup.cfg's install-lib and it all works now :) Thank you both!

Faraz Khan gravatar image Faraz Khan  ( 2018-06-12 22:26:17 -0600 )edit

Question Tools

1 follower


Asked: 2018-06-12 14:19:05 -0600

Seen: 1,596 times

Last updated: Jun 12 '18