Rosanswers logo

Hi, I've been trying to Use my Launch Arguments inside of my Launch File for further purposes. So I can declare it by using launch.actions.DeclareLaunchArgument( 'param1', default_value="defaultString") and access it later using launch.substitutions.LaunchConfiguration( 'param1') to pass it to a node or whatever. The type of this element is a launch.substitutions.launch_configuration.LaunchConfiguration object .

Now I want to use the content of this argument as a string for building filepaths for example. I know several functions are able to interpret the LaunchConfiguration object but I specifically to read it as a string. I have not found a way to do so. As I need to access multiple arguments several times, I am looking for a simple implementation.

Any ideas? Thanks!

(I am working with ROS2 Dashing)

Originally posted by relffok on ROS Answers with karma: 169 on 2019-12-26

Post score: 6

Original comments

Comment by kwang on 2021-02-17:
Hi @relffok, have you figured out how to use a launch argument as a string? I'm trying to pass an argument (file path) to open(yaml_file, 'r').read() function and it gives me the following error when I launch it:

TypeError: expected str, bytes or os.PathLike object, not LaunchConfiguration

2 Answers 2


Rosanswers logo

I don't believe you can access the argument during building of the description. I think you can still do what you want though as far as building paths based on an argument by passing in an array and have the path concat'd at run time.

For example, I want to specify the underlying hardware launch file as an argument. If I give the argument hardware:=rpi1 then my main launch file will include launch file rpi1.launch.py instead of the default hardware.launch.py in it's launch description:

Import required modules:

from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import ThisLaunchFileDir, LaunchConfiguration

Declare the launch argument, then use the launch argument in a build-path statement to include my parameterized launch file:

launch_desc.add_action( launch.actions.DeclareLaunchArgument('hardware', default_value='hardware') )
launch_desc.add_action( IncludeLaunchDescription(PythonLaunchDescriptionSource([ThisLaunchFileDir(),'/',LaunchConfiguration('hardware'), '.launch.py'])) )

Originally posted by guru_florida with karma: 280 on 2019-12-31

This answer was ACCEPTED on the original site

Post score: 1

Original comments

Comment by relffok on 2020-01-02:
Thanks for your answer. I did know the possibility using an array. Unfortunately I need to access the arguments as a string type because the LaunchConfiguration element can only be read by launch action functions and I want to use them with other functions as well.

I can't believe that this is not possible yet because I feel its a very basic and handy action to use.

Comment by KenYN on 2020-06-04:
It appears that Foxy has had the feature added.

Comment by Yanray47 on 2021-04-07:
Is there a good way to get the argument value? Thank you!

Comment by William on 2022-03-14:
@relffok I know this is an old issue, but someone bumped it from another place, and it might help others for me to clarify. Using a substitution is the only way to access the value of the launch argument. You can't just convert it to a string, as it requires the context to be evaluated. If this is confusing, then you might want to stick with xml based launch files as it is more natural to adhere to this restriction there. The Python API is really meant to help implement the markup based frontends like YAML and XML, and so it is declarative rather than imperative.

Hope this helps, and sorry that it's confusing. Others have proposed a more imperative version of the Python API for launch, but that doesn't exist at the moment.


Rosanswers logo

I was able to evaluate launch arguments in a custom function outside of the LaunchDescription by using the OpaqueFunction launch action feature which takes a context as an argument, see this link:


Originally posted by jrgnicho with karma: 486 on 2020-08-24

This answer was NOT ACCEPTED on the original site

Post score: 3


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.