ros2
build_error
ai_generated
true
ImportError: 运行 colcon build --packages-select my_package 时找不到模块 'my_package'
ImportError: No module named 'my_package' when running colcon build --packages-select my_package
ID: ros2/colcon-build-python-import-error-no-module-named-package
85%修复率
84%置信度
1证据数
2023-03-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| ROS2 Humble | active | — | — | — |
| ROS2 Iron | active | — | — | — |
| colcon-core 0.13.x | active | — | — | — |
| colcon-ros 0.5.x | active | — | — | — |
根因分析
ROS2 工作空间中的 Python 包缺少 __init__.py 文件或包结构不正确,导致 colcon 在构建期间无法导入它。
English
The Python package in the ROS2 workspace is missing a __init__.py file or has an incorrect package structure, causing colcon to fail to import it during build.
官方文档
https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Creating-Your-First-ROS2-Package.html解决方案
-
在包的源目录中创建一个空的 __init__.py 文件:'touch src/my_package/my_package/__init__.py'
-
验证包结构:源目录应包含一个与包同名的子目录,其中包含 __init__.py 和其他模块。
无效尝试
常见但无效的做法:
-
60% 失败
The import error is specific to the package; building all packages may mask the issue or fail elsewhere.
-
90% 失败
The error is due to missing __init__.py, not build artifacts; deleting build won't fix it.
-
70% 失败
While this may install the package, it bypasses colcon's build system and may cause inconsistencies in the workspace.