Edit environmental variable with ROS2 python launch file
Hello, I want to edit a environmental variable using my ros2 python launch file. I launch mutiple nodes so setting it once is not an option, as the variable will have to change for each node. Is it possible to set in in the Node description?
test = Node(
package="test",
executable="test",
namespace=namespace,
name="test_node",
output="screen",
emulate_tty=True,
parameters=[params],
)
return LaunchDescription([
test
])
Asked by fnax8902 on 2021-12-01 05:14:31 UTC
Answers
Okay, I found the solution:
from launch.actions import SetEnvironmentVariable
return LaunchDescription([
SetEnvironmentVariable(name='TEST', value='whatever'),
Node(
package="test",
executable="test",
namespace=namespace,
name="test_node",
output="screen",
emulate_tty=True,
)
])
Asked by fnax8902 on 2021-12-01 05:27:50 UTC
Comments
Nevermind, It doesn't set the environmental variable for the specific node. All nodes will have the value of the last launch description called
Asked by fnax8902 on 2021-12-01 06:08:43 UTC
Seems like a correct solution to me, I tried it out. Run print(os.environ.get("TEST")) in your test_node, it prints 'whatever', remove the SetEnvironmentVariable, run again and it should print None. What's your issue?
Asked by tnajjar on 2022-01-10 17:27:10 UTC
it might work with nested LaunchDestriptions
Asked by lucasrj4 on 2023-01-09 06:44:58 UTC
it can be done by using additional_env
ex. from github https://github.com/ros2/launch/blob/rolling/launch/launch/actions/execute_process.py#L104-L122
.. doctest::
>>> ld = LaunchDescription([
... ExecuteProcess(
... cmd=['my_cmd'],
... additional_env={'env_variable': 'env_var_value'},
... ),
... ])
.. code-block:: xml
<launch>
<executable cmd="my_cmd">
<env name="env_variable" value="env_var_value"/>
</executable>
</launch>
"""
is look to be posibale in xml luanchfiles cant fine a way in python
https://design.ros2.org/articles/roslaunch_xml.html#env-tag
Asked by lucasrj4 on 2023-01-09 04:35:33 UTC
Comments