I got an ImportError while trying to create a custom interface following the ros2 tutorial Creating custom msg and srv files. The project looks like this:
src/tutorial_interfaces/
├── CMakeLists.txt
├── include
│ └── tutorial_interfaces
├── msg
│ └── Num.msg
├── package.xml
├── src
└── srv
└── AddThreeInts.srv
I followed all steps but at
step 5 Build the tutorial_interfaces package the command
colcon build --packages-select tutorial_interfaces
failed with the following error message:
Starting >>> tutorial_interfaces
--- stderr: tutorial_interfaces
Traceback (most recent call last):
File "/opt/ros/humble/share/rosidl_generator_py/cmake/../../../lib/rosidl_generator_py/rosidl_generator_py", line 8, in <module>
from rosidl_generator_py import generate_py
ImportError: cannot import name 'generate_py' from 'rosidl_generator_py'
Anyone has an idea how to resolve this?
System: Ubuntu 22.04 +
ROS2 Humble
4
Answers
I’m Facing the same issue (same system)
System: Ubuntu 22.04 + ROS2 Humble
I have the same problem with the same system setup, Ubuntu 22.04 and ROS2 Humble. One more thing is I have anaconda activated. I solved the problem by deactivating conda env and re-build the package. Hopefully, it could help you guys to solve the problem.
I recently had the same error when compiling different packages that had custom interfaces definitions.
I don’t have
anaconda
orconda
installed, so I cannot desactivate it. So I fixed it by commenting this line inCMakeLists.txt
:I’m sure there could be a better way. But I’m not sure if there is a way to debug which version of Python is used by
colcon
and whether or not that version can importgenerate_py
.I fixed this issue by installing dependencies
Basically I looked at the error and saw that it was importing
generate_py
fromrosidl_generator_py
, on my system that was located here:So I went to that directory, booted Python, and tried
There I got more details on the error, where the error basically said I was missing Python dependencies.
So you may be missing more dependencies than me, if so, go to that directory and try to import it yourself, and the interpreter will tell you what you are missing.