# ImportError: 运行 colcon build --packages-select my_package 时找不到模块 'my_package'

- **ID:** `ros2/colcon-build-python-import-error-no-module-named-package`
- **领域:** ros2
- **类别:** build_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

ROS2 工作空间中的 Python 包缺少 __init__.py 文件或包结构不正确，导致 colcon 在构建期间无法导入它。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| ROS2 Humble | active | — | — |
| ROS2 Iron | active | — | — |
| colcon-core 0.13.x | active | — | — |
| colcon-ros 0.5.x | active | — | — |

## 解决方案

1. ```
   在包的源目录中创建一个空的 __init__.py 文件：'touch src/my_package/my_package/__init__.py'
   ```
2. ```
   验证包结构：源目录应包含一个与包同名的子目录，其中包含 __init__.py 和其他模块。
   ```

## 无效尝试

- **** — The import error is specific to the package; building all packages may mask the issue or fail elsewhere. (60% 失败率)
- **** — The error is due to missing __init__.py, not build artifacts; deleting build won't fix it. (90% 失败率)
- **** — While this may install the package, it bypasses colcon's build system and may cause inconsistencies in the workspace. (70% 失败率)
