ROS tf使用报错:ImportError: dynamic module does not define module export function (PyInit_tf使用报错:ImportError: dynamic module does not define module export function (PyInit__tf2)

问题描述

在ROS melodic版本下运行gazebo或者roslaunch时会报错:
ImportError: dynamic module does not define module export function (PyInit__tf2)
导致命令无法执行。

如运行古月ROS入门21讲P17ROS中的坐标管理系统

roslaunch turtle_tf turtle_tf_demo.launch

Alt报错日志

Traceback (most recent call last):
File “/opt/ros/melodic/lib/turtle_tf/turtle_tf_broadcaster.py”, line 37, in
import tf
File “/opt/ros/melodic/lib/python2.7/dist-packages/tf/init.py”, line 30, in
from tf2_ros import TransformException as Exception, ConnectivityException, LookupException, ExtrapolationException
File “/opt/ros/melodic/lib/python2.7/dist-packages/tf2_ros/init.py”, line 38, in
from tf2_py import *
File “/opt/ros/melodic/lib/python2.7/dist-packages/tf2_py/init.py”, line 38, in
from ._tf2 import *
ImportError: dynamic module does not define module export function (PyInit__tf2)
Traceback (most recent call last):
File “/opt/ros/melodic/lib/turtle_tf/turtle_tf_broadcaster.py”, line 37, in
import tf
File “/opt/ros/melodic/lib/python2.7/dist-packages/tf/init.py”, line 30, in
from tf2_ros import TransformException as Exception, ConnectivityException, LookupException, ExtrapolationException
File “/opt/ros/melodic/lib/python2.7/dist-packages/tf2_ros/init.py”, line 38, in
from tf2_py import *
File “/opt/ros/melodic/lib/python2.7/dist-packages/tf2_py/init.py”, line 38, in
from ._tf2 import *
ImportError: dynamic module does not define module export function (PyInit__tf2)
Traceback (most recent call last):
File “/opt/ros/melodic/lib/turtle_tf/turtle_tf_listener.py”, line 37, in
import tf
File “/opt/ros/melodic/lib/python2.7/dist-packages/tf/init.py”, line 30, in
from tf2_ros import TransformException as Exception, ConnectivityException, LookupException, ExtrapolationException
File “/opt/ros/melodic/lib/python2.7/dist-packages/tf2_ros/init.py”, line 38, in
from tf2_py import *
File “/opt/ros/melodic/lib/python2.7/dist-packages/tf2_py/init.py”, line 38, in
from ._tf2 import *
ImportError: dynamic module does not define module export function (PyInit__tf2)
[turtle1_tf_broadcaster-4] process has died [pid 10323, exit code 1, cmd /opt/ros/melodic/lib/turtle_tf/turtle_tf_broadcaster.py __name:=turtle1_tf_broadcaster __log:=/home/chen/.ros/log/24d0e0b8-d140-11ea-b872-74c63b364989/turtle1_tf_broadcaster-4.log].
log file: /home/chen/.ros/log/24d0e0b8-d140-11ea-b872-74c63b364989/turtle1_tf_broadcaster-4*.log
[turtle2_tf_broadcaster-5] process has died [pid 10329, exit code 1, cmd /opt/ros/melodic/lib/turtle_tf/turtle_tf_broadcaster.py __name:=turtle2_tf_broadcaster __log:=/home/chen/.ros/log/24d0e0b8-d140-11ea-b872-74c63b364989/turtle2_tf_broadcaster-5.log].
log file: /home/chen/.ros/log/24d0e0b8-d140-11ea-b872-74c63b364989/turtle2_tf_broadcaster-5*.log
[turtle_pointer-6] process has died [pid 10331, exit code 1, cmd /opt/ros/melodic/lib/turtle_tf/turtle_tf_listener.py __name:=turtle_pointer __log:=/home/chen/.ros/log/24d0e0b8-d140-11ea-b872-74c63b364989/turtle_pointer-6.log].
log file: /home/chen/.ros/log/24d0e0b8-d140-11ea-b872-74c63b364989/turtle_pointer-6*.log

问题原因

出现这种问题一般是由于Ros目前只支持Python2,一些主要的功能包如tf2等都是在Python2的环境下运行的,在Python2中导入相关包不会出现问题,但在Python3中会出错。而目前大部分用户Ubuntu习惯配置和优先使用Python3,所以会导致这类问题出现。

解决方法

ROS melodic 配置 python3:
安装 ROS python3 依赖

sudo apt update
sudo apt install python3-catkin-pkg-modules python3-rospkg-modules python3-empy

编译工作空间

mkdir -p ~/catkin_ws/src; cd ~/catkin_ws
catkin_make
source devel/setup.bash
wstool init
wstool set -y src/geometry2 --git https://github.com/ros/geometry2 -v 0.6.5
wstool up
rosdep install --from-paths src --ignore-src -y -r

完成python3配置

catkin_make --cmake-args
-DCMAKE_BUILD_TYPE=Release
-DPYTHON_EXECUTABLE=/usr/bin/python3
-DPYTHON_INCLUDE_DIR=/usr/include/python3.6m
-DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.s

source 工作空间

ROS 就可以在python3下运行了。
在这里插入图片描述oh,这糟糕的体位。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐