我在尝试按照 ros2 教程创建自定义 msg 和 srv 文件创建自定义界面时遇到导入错误 。该项目如下所示:
\nsrc/tutorial_interfaces/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 CMakeLists.txt\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 include\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 tutorial_interfaces\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 msg\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Num.msg\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.xml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 srv\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 AddThreeInts.srv\nRun Code Online (Sandbox Code Playgroud)\n我遵循了所有步骤,但在\n步骤5 构建tutorial_interfaces 包命令
\ncolcon build --packages-select tutorial_interfaces
失败并显示以下错误消息:
\nsrc/tutorial_interfaces/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 CMakeLists.txt\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 include\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 tutorial_interfaces\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 msg\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Num.msg\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.xml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 srv\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 AddThreeInts.srv\nRun Code Online (Sandbox Code Playgroud)\n任何人都知道如何解决这个问题?
\n\n\n系统:Ubuntu 22.04 +\nROS2 Humble
\n
我通过安装依赖项解决了这个问题
pip install empy
pip install lark
Run Code Online (Sandbox Code Playgroud)
基本上,我查看了错误,发现它是generate_py从rosidl_generator_py位于此处的系统上导入的:
/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_generator_py/__init__.py
Run Code Online (Sandbox Code Playgroud)
所以我进入该目录,启动Python,然后尝试
from generate_py_impl import generate_py
Run Code Online (Sandbox Code Playgroud)
在那里我得到了有关该错误的更多详细信息,该错误基本上表明我缺少 Python 依赖项。
所以你可能比我缺少更多的依赖项,如果是这样,请转到该目录并尝试自己导入它,解释器会告诉你缺少什么。
我面临同样的问题(同一系统)
\n系统:Ubuntu 22.04 + ROS2 Humble
\ncolcon build \xe2\x94\x80\xe2\x95\xaf\n[0.370s] WARNING:colcon.colcon_core.package_selection:Some selected packages are already built in one or more underlay workspaces:\n 'turtlesim' is in: /opt/ros/humble\nIf a package in a merged underlay workspace is overridden and it installs headers, then all packages in the overlay must sort their include directories by workspace order. Failure to do so may result in build failures or undefined behavior at run time.\nIf the overridden package is used by another package in any underlay, then the overriding package in the overlay must be API and ABI compatible or undefined behavior at run time may occur.\n\nIf you understand the risks and want to override a package anyways, add the following to the command line:\n --allow-overriding turtlesim\n\nThis may be promoted to an error in a future release of colcon-override-check.\nStarting >>> turtlesim\n--- stderr: turtlesim \nTraceback (most recent call last):\n File "/opt/ros/humble/share/rosidl_generator_py/cmake/../../../lib/rosidl_generator_py/rosidl_generator_py", line 8, in <module>\n from rosidl_generator_py import generate_py\nImportError: cannot import name 'generate_py' from 'rosidl_generator_py' (/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_generator_py/__init__.py)\ngmake[2]: *** [turtlesim__py/CMakeFiles/turtlesim__py.dir/build.make:121: rosidl_generator_py/turtlesim/_turtlesim_s.ep.rosidl_typesupport_fastrtps_c.c] Error 1\ngmake[1]: *** [CMakeFiles/Makefile2:706: turtlesim__py/CMakeFiles/turtlesim__py.dir/all] Error 2\ngmake[1]: *** Waiting for unfinished jobs....\ngmake: *** [Makefile:146: all] Error 2\n---\nFailed <<< turtlesim [0.48s, exited with code 2]\n\nSummary: 0 packages finished [0.64s]\n 1 package failed: turtlesim\n 1 package had stderr output: turtlesim\nRun Code Online (Sandbox Code Playgroud)\n