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

ImportError: dynamic module does not define init function (init_tf2)

asked 2019-12-31 22:11:07 -0600

svdeepak99 gravatar image

Hello. I am trying to "import tf" in a python 2.7 terminal, but I am getting the following error. Please help me up, as I am unable to proceed further:

svdeepak99@Deepak-Strix:~/catkin_ws$ python
Python 2.7.17 (default, Nov  7 2019, 10:07:09) 
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/ros/melodic/lib/python2.7/dist-packages/tf/", line 30, in <module>
    from tf2_ros import TransformException as Exception, ConnectivityException, LookupException, ExtrapolationException
  File "/home/svdeepak99/catkin_ws/devel/lib/python3/dist-packages/tf2_ros/", line 35, in <module>
  File "<string>", line 38, in <module>
  File "/home/svdeepak99/catkin_ws/devel/lib/python3/dist-packages/tf2_py/", line 35, in <module>
  File "<string>", line 38, in <module>
ImportError: dynamic module does not define init function (init_tf2)

I am using ros melodic and Ubuntu 18.04 version. Thanks in advance.

edit retag flag offensive close merge delete



You seem to be mixing Python 2 and Python 3 (note python2.7/dist-packages/tf and python3/dist-packages/tf2_ros mentioned in the error).

You'll have to make sure not to do that.

gvdhoorn gravatar image gvdhoorn  ( 2020-01-01 05:56:22 -0600 )edit

Thanks for the reply. I tried uninstalling & reinstalling tf & tf2 packages using apt-get multiple number of times, but that doesn't solve my issue. Could you please suggest me how to solve this?

svdeepak99 gravatar image svdeepak99  ( 2020-01-01 10:39:47 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2020-01-02 13:20:53 -0600

tfoote gravatar image

We have worked hard to make sure that the code can be run in python 2 or python 3. But you must choose one and remain consistent. Mixing them will not work.

As @gvdhoorn mentioned you are mixing your environment with python2 and python3 instances. To resolve this make sure to consistently use the same python interpreter in any workspace. Note that for melodic the default python interpreter is python2.

If you used a different python interpreter to build or install a workspace you will want to completely clean the generated outputs and rebuild your workspace with the version of python you want.

edit flag offensive delete link more


Thank you so much. That solved my problem. I just took backup of the files in "src" folder, then deleted the catkin_ws folder, created a new catkin workspace, copied the backed up files into the new "src" folder and ran "catkin_make" command. The error is solved now.

svdeepak99 gravatar image svdeepak99  ( 2020-02-12 04:42:22 -0600 )edit

answered 2022-09-05 04:38:35 -0600

dereje gravatar image

Thank you very much. First I copied all my files in src folder, abd the I deleted my working space. Then I created new ws and copied up all my files into the new src folder. finally i run command "source devel/setup.bash and catkin_make"

edit flag offensive delete link more

Question Tools


Asked: 2019-12-31 22:05:28 -0600

Seen: 6,557 times

Last updated: Jan 02 '20