ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

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/", line 8, in <module>
    import tf
  File "/home/bjh/catkin_ws/devel/lib/python2.7/dist-packages/tf/", line 35, in <module>
  File "<string>", line 30, in <module>
  File "/home/bjh/catkin_ws/devel/lib/python2.7/dist-packages/tf2_ros/", line 35, in <module>
  File "<string>", line 38, in <module>
  File "/home/bjh/catkin_ws/devel/lib/python2.7/dist-packages/tf2_py/", line 35, in <module>
  File "<string>", line 38, in <module>
ImportError: dynamic module does not define module export function (PyInit__tf2)
edit retag flag offensive close merge delete

5 Answers

Sort by ยป oldest newest most voted

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
source devel/setup.bash
wstool init
wstool set -y src/geometry2 --git -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 \

Do not forget to always source your workspace!

edit flag offensive delete link more


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/

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

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 does not exist ... it works despite of it

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

@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/

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

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


This worked perfectly. The issue was that by default my python version was python 3.8.10, but ROS melodic requires python 2.7.

Using "which python" in the terminal showed me this.

hunterlineage1 gravatar image hunterlineage1  ( 2023-07-24 19:05:10 -0500 )edit

answered 2022-04-20 03:47:27 -0500

distro gravatar image

@r7vme I followed your instrction and have geometry2 in my workspace but I dont know how to get my system to actually use the tf2 from this new geometry 2. In my codes do I uses #!/usr/bin/env python3 or do I change 3 to 2.7? I'm confused please help.

edit flag offensive delete link more

answered 2022-03-12 07:39:11 -0500

medfa1 gravatar image

What you need to do is to make sure that you have the write PATH & PYTHONPATH by exporting the right paths

export PATH="/root/anaconda3/bin:$PATH"

export PYTHONPATH="/root/anaconda3/envs/venv/bin/python3.6"
edit flag offensive delete link more

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/ --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/ --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

Question Tools



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

Seen: 51,729 times

Last updated: Apr 20 '22