小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
关于conda环境和ros2的联合使用
-
目前在复现graspnet算法,我将该算法环境全部部署在了conda环境中,目前想使用ros2来进行话题通讯,其中conda环境中python版本为3.7,ros2使用的是系统的python版本为3.10,目前以及试过了鱼总的方法,比如
export PYTHONPATH="/home/liu/anaconda3/envs/graspnet/lib/python3.7/site-packages"
以及社区内其他同学的方法,比如
sys.path.append('/home/liu/anaconda3/envs/graspnet/lib/python3.7/site-packages/')
都不太行,会出现以下问题
Traceback (most recent call last): File "/home/liu/anaconda3/envs/graspnet/lib/python3.7/site-packages/numpy/core/__init__.py", line 22, in <module> from . import multiarray File "/home/liu/anaconda3/envs/graspnet/lib/python3.7/site-packages/numpy/core/multiarray.py", line 12, in <module> from . import overrides File "/home/liu/anaconda3/envs/graspnet/lib/python3.7/site-packages/numpy/core/overrides.py", line 7, in <module> from numpy.core._multiarray_umath import ( ModuleNotFoundError: No module named 'numpy.core._multiarray_umath' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/liu/graspness_implementation/graspness_ros_ws/install/matrix_topic/lib/matrix_topic/matrix_publisher", line 33, in <module> sys.exit(load_entry_point('matrix-topic==0.0.0', 'console_scripts', 'matrix_publisher')()) File "/home/liu/graspness_implementation/graspness_ros_ws/install/matrix_topic/lib/matrix_topic/matrix_publisher", line 25, in importlib_load_entry_point return next(matches).load() File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 171, in load module = import_module(match.group('module')) File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1050, in _gcd_import File "<frozen importlib._bootstrap>", line 1027, in _find_and_load File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 688, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 883, in exec_module File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed File "/home/liu/graspness_implementation/graspness_ros_ws/install/matrix_topic/lib/python3.10/site-packages/matrix_topic/matrix_pub.py", line 7, in <module> import numpy as np File "/home/liu/anaconda3/envs/graspnet/lib/python3.7/site-packages/numpy/__init__.py", line 150, in <module> from . import core File "/home/liu/anaconda3/envs/graspnet/lib/python3.7/site-packages/numpy/core/__init__.py", line 48, in <module> raise ImportError(msg) ImportError: IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE! Importing the numpy C-extensions failed. This error can happen for many reasons, often due to issues with your setup or how NumPy was installed. We have compiled some common reasons and troubleshooting tips at: https://numpy.org/devdocs/user/troubleshooting-importerror.html Please note and check the following: * The Python version is: Python3.10 from "/usr/bin/python3" * The NumPy version is: "1.21.0" and make sure that they are the versions you expect. Please carefully study the documentation linked above for further help. Original error was: No module named 'numpy.core._multiarray_umath' [ros2run]: Process exited with failure 1
也已经在系统中装了numpy1.21.0了 但是还是出现问题,难道conda环境和ros2不能联合调用吗
-
在您的场景中,问题主要来源于Python环境的冲突。您正在尝试将Conda环境中的库与ROS 2(使用系统Python环境)结合使用,这通常会导致兼容性问题,因为不同Python版本的库(特别是像NumPy这样的底层库)之间的二进制接口可能不兼容。
解决方案
-
使用单一的Python环境:
最简单和最推荐的解决方案是选择一个Python环境(Conda或系统Python),并在该环境中安装所有必要的库。如果您倾向于使用Conda,您可以在Conda环境中安装ROS 2(这可能需要一些额外的步骤,因为ROS 2通常不在Conda的默认源中)。 -
在Conda环境中运行ROS 2:
如果您想继续使用Conda环境,并希望在该环境中运行ROS 2,您可以尝试以下步骤:- 在Conda环境中安装ROS 2及其依赖。这可能需要从源代码编译ROS 2,或者找到已经为Conda打包的ROS 2版本。
- 确保Conda环境中的Python版本与ROS 2的要求相匹配。
-
使用虚拟环境(如果可能的话):
如果您不能或不想在Conda环境中运行ROS 2,您可以在系统Python中创建一个虚拟环境,专门用于ROS 2,并在其中安装所有ROS 2相关的库。然后,您可以通过确保PYTHONPATH
和PATH
环境变量正确设置来在需要时从Conda环境导入库。 -
动态调整
PYTHONPATH
:
如果您只是想临时在ROS 2脚本中使用Conda环境中的某些库,您可以在ROS 2的Python脚本中动态调整sys.path
。但请注意,这种方法可能不稳定,特别是当库之间存在复杂的依赖关系时。示例代码:
import sys sys.path.append('/home/liu/anaconda3/envs/graspnet/lib/python3.7/site-packages') import numpy # 确保numpy从Conda环境加载
-
检查NumPy版本:
由于NumPy在Python 3.7和3.10之间可能存在二进制不兼容的问题,请确保您在所有环境中安装的NumPy版本都是针对当前Python版本编译的。 -
考虑使用Docker:
使用Docker可以提供一个完全隔离的环境,您可以在其中安装所有必要的软件和库,而无需担心环境冲突。
结论
通常,最好避免在多个Python环境中混合使用库,因为这可能会导致难以调试的问题。如果可能的话,选择一个环境并坚持使用它。如果必须使用多个环境,请确保它们之间的依赖关系得到妥善处理。
以上为生成回答,仅供参考~
-
-
还有这个错误
Traceback (most recent call last): File "/home/liu/graspness_implementation/graspness_ros_ws/install/matrix_topic/lib/matrix_topic/matrix_publisher", line 33, in <module> sys.exit(load_entry_point('matrix-topic==0.0.0', 'console_scripts', 'matrix_publisher')()) File "/home/liu/graspness_implementation/graspness_ros_ws/install/matrix_topic/lib/matrix_topic/matrix_publisher", line 25, in importlib_load_entry_point return next(matches).load() File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 171, in load module = import_module(match.group('module')) File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1050, in _gcd_import File "<frozen importlib._bootstrap>", line 1027, in _find_and_load File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 688, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 883, in exec_module File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed File "/home/liu/graspness_implementation/graspness_ros_ws/install/matrix_topic/lib/python3.10/site-packages/matrix_topic/matrix_pub.py", line 10, in <module> import demo_norobot File "/home/liu/graspness_implementation/demo_norobot.py", line 9, in <module> import torch File "/home/liu/anaconda3/envs/graspnet/lib/python3.7/site-packages/torch/__init__.py", line 214, in <module> raise ImportError(textwrap.dedent(''' ImportError: Failed to load PyTorch C extensions: It appears that PyTorch has loaded the `torch/_C` folder of the PyTorch repository rather than the C extensions which are expected in the `torch._C` namespace. This can occur when using the `install` workflow. e.g. $ python setup.py install && python -c "import torch" This error can generally be solved using the `develop` workflow $ python setup.py develop && python -c "import torch" # This should succeed or by running Python from a different directory. [ros2run]: Process exited with failure 1
-
@2215650560 在 关于conda环境和ros2的联合使用 中说:
export PYTHONPATH="/home/liu/anaconda3/envs/graspnet/lib/python3.7/site-packages"
改一下:
export PYTHONPATH=/home/liu/anaconda3/envs/graspnet/lib/python3.7/site-packages:$PYTHONPATH
-
@小鱼 还是一样报错误
-
请问你解决了这个问题吗
-
预编译包安装的ros2似乎都是强绑定你系统自带的python版本(和ubuntu系统版本有关)。ros2 run的时候也是默认用该版本的python解释器。所以如果你conda环境里有某个包也强绑定另一python版本,那就会报错。我目在rk3588板上跑算法,conda环境里装好的rknn包所需python、和ros2所需python不同,导致没法调试。
目前看来最好的办法,要么其它所有包都与ros2的python版本对齐,要么不要预编译安装,而是采用源码编译安装方式。注意后者非常吃你的网络和vpn稳定性。
以上是个人试错经验,希望大佬能给出更简单的方法解决