skip to Main Content

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


  1. I’m Facing the same issue (same system)

    System: Ubuntu 22.04 + ROS2 Humble

    colcon build                                                                                          ─╯
    [0.370s] WARNING:colcon.colcon_core.package_selection:Some selected packages are already built in one or more underlay workspaces:
        'turtlesim' is in: /opt/ros/humble
    If a package in a merged underlay workspace is overridden and it installs headers, then all packages in the overlay must sort their include directories by workspace order. Failure to do so may result in build failures or undefined behavior at run time.
    If the overridden package is used by another package in any underlay, then the overriding package in the overlay must be API and ABI compatible or undefined behavior at run time may occur.
    
    If you understand the risks and want to override a package anyways, add the following to the command line:
        --allow-overriding turtlesim
    
    This may be promoted to an error in a future release of colcon-override-check.
    Starting >>> turtlesim
    --- stderr: turtlesim                             
    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' (/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_generator_py/__init__.py)
    gmake[2]: *** [turtlesim__py/CMakeFiles/turtlesim__py.dir/build.make:121: rosidl_generator_py/turtlesim/_turtlesim_s.ep.rosidl_typesupport_fastrtps_c.c] Error 1
    gmake[1]: *** [CMakeFiles/Makefile2:706: turtlesim__py/CMakeFiles/turtlesim__py.dir/all] Error 2
    gmake[1]: *** Waiting for unfinished jobs....
    gmake: *** [Makefile:146: all] Error 2
    ---
    Failed   <<< turtlesim [0.48s, exited with code 2]
    
    Summary: 0 packages finished [0.64s]
      1 package failed: turtlesim
      1 package had stderr output: turtlesim
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. I recently had the same error when compiling different packages that had custom interfaces definitions.

    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'
    

    I don’t have anaconda or conda installed, so I cannot desactivate it. So I fixed it by commenting this line in CMakeLists.txt:

    # find_package(rosidl_default_generators REQUIRED)
    

    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 import generate_py.

    Login or Signup to reply.
  4. I fixed this issue by installing dependencies

    pip install empy
    pip install lark
    

    Basically I looked at the error and saw that it was importing generate_py from rosidl_generator_py, on my system that was located here:

    /opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_generator_py/__init__.py
    

    So I went to that directory, booted Python, and tried

    from generate_py_impl import generate_py
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search