COL-1002 ros2 test_error ai_generated true

ModuleNotFoundError: No module named 'my_package' during colcon test

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

Also available as: JSON · Markdown · 中文
88%Fix Rate
87%Confidence
1Evidence
2023-11-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
ROS2 Foxy Fitzroy active
ROS2 Galactic Geochelone active
ROS2 Humble Hawksbill active

Root Cause

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

中文

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

Official Documentation

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

Workarounds

  1. 90% success 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`.
    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. 85% success 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.
    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. 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.

Dead Ends

Common approaches that don't work:

  1. 50% fail

    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% fail

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