ROS2 Humble教程运行报错:无法导入launch.substitutions的FileContent
问题原因
报错核心是FileContent类在ROS2 Humble的launch.substitutions模块中不存在——该类是ROS2 Iron及后续版本才引入的新特性,你使用的Humble版本不支持,直接照搬高版本教程代码会触发兼容性错误。
解决方法
修改你的launch.py文件,将FileContent替换为Humble兼容的实现逻辑:
- 定位原教程中的问题代码片段:
from launch.substitutions import FileContent ... robot_description = ParameterValue( FileContent(Command(['xacro ', get_package_share_path('urdf_tutorial_cpp'), '/description/robot.urdf.xacro'])), value_type=str )
- 替换为Humble支持的代码:
from launch.substitutions import Command ... robot_description = ParameterValue( Command(['xacro ', str(get_package_share_path('urdf_tutorial_cpp')), '/description/robot.urdf.xacro']), value_type=str )
- 重新编译并生效:
colcon build --packages-select urdf_tutorial_cpp source install/setup.bash
- 再次执行launch命令:
ros2 launch urdf_tutorial_cpp launch.py
额外提示
ROS2不同版本的launch API存在差异,Humble作为长期支持版本,部分新特性并未包含。后续跟随教程时,务必确认教程对应的ROS2版本与你使用的版本一致,避免类似兼容性问题。
内容的提问来源于stack exchange,提问作者Searcher
相关产品推荐
相关产品推荐

