COL-1002 ros2 test_error ai_generated true

ModuleNotFoundError: colcon test 期间找不到名为 'my_package' 的模块

ModuleNotFoundError: No module named 'my_package' during colcon test

ID: ros2/colcon-test-import-error-pkg-name

其他格式: JSON · Markdown 中文 · English
88%修复率
87%置信度
1证据数
2023-11-20首次发现

版本兼容性

版本状态引入弃用备注
ROS2 Foxy Fitzroy active
ROS2 Galactic Geochelone active
ROS2 Humble Hawksbill active

根因分析

setup.py 或 setup.cfg 中的 Python 包名称与实际目录名称不匹配,或者由于缺少 --symlink-install 或构建路径问题,包未安装到 Python 环境中。

English

The Python package name in setup.py or setup.cfg does not match the actual directory name, or the package is not installed in the Python environment due to missing --symlink-install or build path issues.

generic

官方文档

https://docs.ros.org/en/humble/Tutorials/Basic/Colcon-Tutorial.html

解决方案

  1. Ensure the Python package name in setup.py matches the directory name. For example, if your package directory is 'my_package', set `name='my_package'` in setup.py. Then rebuild with `colcon build --symlink-install` and run tests with `colcon test --packages-select my_package`.
  2. If using ament_python, verify that the `entry_points` in setup.py are correct and that the test directory has an `__init__.py` file. Run `colcon test --event-handlers console_direct+` to see the full error trace.

无效尝试

常见但无效的做法:

  1. 50% 失败

    Running pip install -e . in the package root may install it temporarily, but colcon will overwrite it on next build, and it doesn't fix the root cause.

  2. 30% 失败

    Deleting the entire build directory and rebuilding is overkill; the issue is usually a configuration mismatch, not a corrupted build.