Ask Your Question
5

ImportError: dynamic module does not define module export function (PyInit__tf2)

asked 2019-06-19 01:52:50 -0500

joonhyup gravatar image

updated 2019-06-19 01:59:50 -0500

gvdhoorn gravatar image

When I tried to do tutorial of tf, I got an error as follw. And same error is occurred when I tried to do the tutorial of tf2.

How can I fix the problem? Thanks

Traceback (most recent call last):
  File "/home/bjh/catkin_ws/src/learning_tf2/nodes/static_turtle_tf2_broadcaster.py", line 8, in <module>
    import tf
  File "/home/bjh/catkin_ws/devel/lib/python2.7/dist-packages/tf/__init__.py", line 35, in <module>
    exec(__fh.read())
  File "<string>", line 30, in <module>
  File "/home/bjh/catkin_ws/devel/lib/python2.7/dist-packages/tf2_ros/__init__.py", line 35, in <module>
    exec(__fh.read())
  File "<string>", line 38, in <module>
  File "/home/bjh/catkin_ws/devel/lib/python2.7/dist-packages/tf2_py/__init__.py", line 35, in <module>
    exec(__fh.read())
  File "<string>", line 38, in <module>
ImportError: dynamic module does not define module export function (PyInit__tf2)
edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2021-07-06 13:04:36 -0500

siddharthcb gravatar image

errors like these are also caused by python version problems. so change the first line of the script from #! /usr/bin/env python to #! /usr/bin/env python2.7 worked for me

edit flag offensive delete link more
19

answered 2019-06-19 15:52:14 -0500

r7vme gravatar image

Issue is because tf2_ros was compiled for python2. Detailed discussion here.

To recompile for python3 (melodic):

Install some prerequisites to use Python3 with ROS.

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

Prepare catkin workspace

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

Finally compile for Python 3

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.so

Do not forget to always source your workspace!

edit flag offensive delete link more

Comments

I am very appreciate for your help! It is done!

joonhyup gravatar image joonhyup  ( 2019-06-20 18:15:30 -0500 )edit

Good to hear! @joonhyup can you please accept answer if it helped?

r7vme gravatar image r7vme  ( 2019-06-20 21:18:42 -0500 )edit

If your platform is not x86_64, don't forget to change to -DPYTHON_LIBRARY=/usr/lib/YOUR_ARCH_NAME-linux-gnu/libpython3.6m.so

Carls gravatar image Carls  ( 2020-03-26 04:32:06 -0500 )edit

Also, I've got to install numpy by sudo apt-get install python3-numpy

Carls gravatar image Carls  ( 2020-03-26 04:48:27 -0500 )edit
2

Hi. I just resolved the PyInit_tf2 error and now I am facing the PyInit_PyKDL error. I tried to do the same way for resolving PyInit_tf2 but it fails when executing wstool up. I would like to know is there any way to resolve the PyInit_PyKDL? Thanks.

DJ gravatar image DJ  ( 2020-04-26 21:04:53 -0500 )edit

how about if we're using catkin build

azerila gravatar image azerila  ( 2020-05-20 15:41:00 -0500 )edit

libpython3.6m.so does not exist ... it works despite of it

jester gravatar image jester  ( 2020-07-30 08:07:47 -0500 )edit
1

@azerila, catkin build --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.so

dendrarij gravatar image dendrarij  ( 2020-08-04 07:32:42 -0500 )edit
0

answered 2021-07-12 08:43:45 -0500

zlg9 gravatar image

updated 2021-07-12 08:53:55 -0500

In this approach, it is assumed that you use catkin_make. You will first need to verify your python3 version (run python3 --version), its installation path as well as your catkin workspace path and modify the following commands if needed. What follows holds for python3.6 on Ubuntu 18.04 (aarch64).

Navigate to your catkin_ws root directory:

cd /home/$USER/catkin_ws

Rebuild geometry with python3 (if you have installed it already):

catkin_make -DPYTHON_EXECUTABLE:FILEPATH=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.6m.so --only-pkg-with-deps geometry

And rebuild geometry2:

catkin_make -DPYTHON_EXECUTABLE:FILEPATH=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.6m.so --only-pkg-with-deps geometry2

You will only need to rebuild tf2_py to address issues with PyInit__tf2. You can either rebuild the whole geometry2 package as mentioned above or just rebuild tf2_py with python3. In my case, I have already both geometry and geometry2 on my ROS, so I rebuilt both.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

3 followers

Stats

Asked: 2019-06-19 01:52:50 -0500

Seen: 24,859 times

Last updated: Jul 12