ros2 launch: 替换错误:在PATH或启动目录中找不到命令'my_script.sh'
ros2 launch: Substitution error: command 'my_script.sh' not found in PATH or launch directory
ID: ros2/ros2-launch-substitution-error-command-not-found
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Humble | active | — | — | — |
| Iron | active | — | — | — |
| Rolling | active | — | — | — |
根因分析
启动文件使用命令替换(如FindExecutable或ExecuteProcess)来定位可执行文件,但该可执行文件不在系统PATH或启动文件的包共享目录中。
English
The launch file uses a command substitution (e.g., FindExecutable or ExecuteProcess) to locate an executable, but the executable is not in the system PATH or the launch file's package share directory.
官方文档
https://docs.ros.org/en/rolling/Tutorials/Intermediate/Launch/Using-Environment-Variables.html解决方案
-
Ensure the executable is in the PATH. For a shell script in your package, add it to the package's CMakeLists.txt: install(PROGRAMS scripts/my_script.sh DESTINATION lib/${PROJECT_NAME}). Then source the workspace and run: ros2 launch my_package my_launch.py -
Use FindExecutable substitution with the correct package and executable name. In launch file: from launch.substitutions import FindExecutable; executable = FindExecutable(name='my_script.sh'). Ensure the script is installed in the package's lib directory.
-
If the script is external, add its directory to PATH before launching: export PATH=$PATH:/path/to/script/dir && ros2 launch my_package my_launch.py
无效尝试
常见但无效的做法:
-
90% 失败
Even if the script is in the launch directory, the substitution uses the package's share directory, not the source directory. You must install the script via CMake or set the PATH.
-
80% 失败
Absolute paths work only on the specific machine they were written for. They break when the workspace is moved or shared with other developers.