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

What is the workflow to prevent mixing python2.7 and python3 for tf2?

asked 2020-01-28 15:33:56 -0500

gmetts gravatar image

updated 2020-01-28 17:46:18 -0500

jayess gravatar image

I have chronically had the issue where I had to run python2 if I ever wanted to use the tf2 libraries. I use primarily kinetic+16.04 and melodic+18.04. I typically get the issue:

>>> import tf2_ros

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/ros/melodic/lib/python2.7/dist-packages/tf2_ros/", line 38, in <module>
    from tf2_py import *
  File "/opt/ros/melodic/lib/python2.7/dist-packages/tf2_py/", line 38, in <module>
    from ._tf2 import *
ImportError: dynamic module does not define module export function (PyInit__tf2)

I've seen it documented that this issue is caused by mixing python2 and python3, which I understand. However, I haven't found a good posting, documentation, or guide on how to override the tendency for python2 to cause issues by default, as melodic uses 2.7 from the start. With the shift to python3 officially in full force, I wanted to finally figure out how to do this, as I will likely still have to use kinetic and melodic for the foreseeable future and won't be able to immediately adopt noetic and ROS2 due to lab congruity, but I don't want the current packages I'm writing to be python2 dependent. I've had numerous students I advise run into the same problem and I haven't been able to help them. Assuming a clean install of Ubuntu 18.04+Melodic, how would I go about running tf2 on python3? I'd like to document this and circulate it around my university, as it is a common issue that many people simply avoid rather than try to fix, and is preventing some people from doing away with python2, as I have done up until this point. I'd appreciate if anyone on the geometry2 team could assist, and I'd be glad to return a write up guide to help people that are in a similar position.

Thanks in advance

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-01-28 17:52:08 -0500

tfoote gravatar image

The solution to not mixing python2 and python3 is to have workspaces with only python2 or python3 and to never invoke the other python within that system. You will have to build everything from source as the debian packages are built for the default version of python (2) in melodic.

The simplest way to do this is to setup a virualenv and then do the standard compile from source.

If you don't use the virtualenv there are a few paths and settings that you may need to update.

I found a tutorial here that looks like it's pretty up to date:

There's also several answers on like

But in general this isn't a supported target so it's not a process for which we're optimizing.

Related to the fact that this isn't supported. At a higher level I'd recommend that for the duration of the remaining period of support for Melodic that you simply write your code to be compatible with both python2 and python3. It's takes a relatively low overhead and all of the core packages are written that way. We have a guide to do this at:

edit flag offensive delete link more


Thank you Tully for your response. I completely understand that with Noetic and ROS2 on the horizon, it doesn't make sense to optimize processes that will soon be outdated. It'll be nice to have an option if we are forced to run a python3 dependant instance of tf2 on kinetic or melodic in the future. Thanks again for your help.

gmetts gravatar image gmetts  ( 2020-01-30 14:32:25 -0500 )edit

Question Tools



Asked: 2020-01-28 15:33:56 -0500

Seen: 4,150 times

Last updated: Jan 28 '20