Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Because of the relatively loose coupling between a source file and the package it resides it, I don't think it is easy to find out the package name from within the source file. You could do it by looking for specific parent directories, but this will need to be different depending on if the file has been installed or not (if not, then look for the nearest parent with a package.xml file, if so and installed like a binary then look for the immediate parent directory of the executable). I think this is very fragile, however.

If you don't want to hard-code the package name, which is understandable, and would prefer not to use relative paths inside the package (which might break if the package gets installed), then the best I can come up with is to receive the package name as a command line argument or a ROS parameter inside the script. Then you can specify the package name on the command line or in the launch file. This is really only moving the hard-coding somewhere else, though, and if the script is intended to be run from the command line it would be a pain to use. I think that if the script is always run from a launch file, I would consider putting the package name in a parameter but I do not think the maintenance trade-off is that beneficial. If it's run from the command line, I would personally prefer to hard-code the package name in the script.

By the way, the resource_retriever package is often useful when loading resources, both local and remote. (It won't solve your hard-code-the-package-name problem, though.)