ROS2 Importerror:无法导入名称' generate_py'从' rosidl_generator_py'

发布于 2025-02-10 14:19:50 字数 1342 浏览 3 评论 0原文

我在尝试创建一个自定义界面的同时,按照ROS2教程创建自定义msg和srv文件。该项目看起来像这样:

src/tutorial_interfaces/
├── CMakeLists.txt
├── include
│   └── tutorial_interfaces
├── msg
│   └── Num.msg
├── package.xml
├── src
└── srv
    └── AddThreeInts.srv

我遵循所有步骤,但是 步骤 5构建tutorial_interfaces软件包命令

colcon build -packages-select tutorial_interfaces

失败了以下错误消息:

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'

有人知道如何解决这个问题吗?

系统:Ubuntu 22.04 + ROS2 Humble

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月如刀 2025-02-17 14:19:50

通过安装依赖项来解决此问题,

pip install empy
pip install lark

我基本上查看了错误,并在位于此处的系统上看到了它是从rosidl_generator_py中导入generate_py

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

我 目录,启动Python,并尝试了

from generate_py_impl import generate_py

那里,我得到了有关错误的更多详细信息,错误基本上说我缺少Python依赖关系。

因此,您可能会缺少比我更多的依赖关系,如果是的话,请转到该目录并尝试自己导入它,口译员会告诉您您所缺少的内容。

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.

海螺姑娘 2025-02-17 14:19:50

我面临同一问题(同一系统)

系统: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

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
以歌曲疗慰 2025-02-17 14:19:50

我在同一系统设置Ubuntu 22.04和Ros2 Humble上也有同样的问题。还有一件事是,我激活了Anaconda。我通过停用conda env并重新构建包装来解决问题。希望它可以帮助大家解决问题。

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.

小鸟爱天空丶 2025-02-17 14:19:50

最近,我在编译具有自定义接口定义的不同软件包时遇到了相同的错误。

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'

我没有anacondaconda已安装,所以我无法脱离它。因此,我通过在cmakelists.txt中评论此行来修复它:

# find_package(rosidl_default_generators REQUIRED)

我敢肯定可能有更好的方法。但是我不确定是否有一种方法可以通过colcon使用哪种版本的Python,并且该版本是否可以导入generate_py

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文