当尝试构建我的 ROS2 项目时,由于缺少名为
ModuleNotFoundError
的依赖项,我在编译其中一个 C 模块时得到了 em
:
% colcon build --cmake-clean-cache
Starting >>> r1_messages
--- stderr: r1_messages
CMake Error at /Users/mryall/src/mawson/ros2-iron-build/install/share/rosidl_adapter/cmake/rosidl_adapt_interfaces.cmake:57 (message):
execute_process(/opt/homebrew/Frameworks/Python.framework/Versions/3.12/bin/python3.12
-m rosidl_adapter --package-name r1_messages --arguments-file
/Users/mryall/src/mawson/r1-ros/build/r1_messages/rosidl_adapter__arguments__r1_messages.json
--output-dir
/Users/mryall/src/mawson/r1-ros/build/r1_messages/rosidl_adapter/r1_messages
--output-file
/Users/mryall/src/mawson/r1-ros/build/r1_messages/rosidl_adapter/r1_messages.idls)
returned error code 1:
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/Users/mryall/src/mawson/ros2-iron-build/install/lib/python3.11/site-packages/rosidl_adapter/__main__.py", line 19, in <module>
sys.exit(main())
^^^^^^
File "/Users/mryall/src/mawson/ros2-iron-build/install/lib/python3.11/site-packages/rosidl_adapter/main.py", line 53, in main
abs_idl_file = convert_to_idl(
^^^^^^^^^^^^^^^
File "/Users/mryall/src/mawson/ros2-iron-build/install/lib/python3.11/site-packages/rosidl_adapter/__init__.py", line 18, in convert_to_idl
from rosidl_adapter.msg import convert_msg_to_idl
File "/Users/mryall/src/mawson/ros2-iron-build/install/lib/python3.11/site-packages/rosidl_adapter/msg/__init__.py", line 16, in <module>
from rosidl_adapter.resource import expand_template
File "/Users/mryall/src/mawson/ros2-iron-build/install/lib/python3.11/site-packages/rosidl_adapter/resource/__init__.py", line 19, in <module>
import em
ModuleNotFoundError: No module named 'em'
Call Stack (most recent call first):
/Users/mryall/src/mawson/ros2-iron-build/install/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:132 (rosidl_adapt_interfaces)
CMakeLists.txt:14 (rosidl_generate_interfaces)
---
Failed <<< r1_messages [4.13s, exited with code 1]
我的 ROS2 安装是使用 Python 虚拟环境构建的,我已激活该环境。但是,即使激活了 virtual-env 并且使用
pip
安装了所有必需的库,但使用 CMake 构建的 ROS 模块似乎无法找到它。
我尝试检查
em
库是否已安装。运行 pip install empy
确认已安装:
% pip install empy
Requirement already satisfied: empy in /Users/mryall/src/mawson/ros2-iron/iron_venv/lib/python3.11/site-packages (3.3.4)
ROS2 中的 CMake 模块不会自动选取调用
colcon
的 shell 使用的虚拟环境。 (这显然是 ROS2 维护者有意为之的决定,他们似乎不太了解虚拟环境的目的或操作。)
您可以通过要求
colcon
将 Python3_EXECUTABLE
参数传递给 CMake 来修复此问题,并使用 shell 中的 $VIRTUAL_ENV
环境变量和活动的 virtualenv:
% colcon build --cmake-args -DPython3_EXECUTABLE="$VIRTUAL_ENV/bin/python"
幸运的是,一旦知道了正确的二进制文件,Python 就足够聪明,可以找到安装在 virtualenv 中的库。
这使得 ROS2 代码中的 C++/CMake 模块能够找到其所有 Python 依赖项。