0
$\begingroup$

Rosanswers logo

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/__init__.py", line 38, in <module>
    from tf2_py import *
  File "/opt/ros/melodic/lib/python2.7/dist-packages/tf2_py/__init__.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


Originally posted by gmetts on ROS Answers with karma: 3 on 2020-01-28

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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. https://docs.python.org/3/tutorial/venv.html

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

http://wiki.ros.org/UsingPython3/BuildUsingPython3

I found a tutorial here that looks like it's pretty up to date: https://www.miguelalonsojr.com/blog/robotics/ros/python3/2019/08/20/ros-melodic-python-3-build.html

There's also several answers on answers.ros.org like https://answers.ros.org/question/237613/how-to-define-ros-kinetic-to-use-python3-instead-of-python27/

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: http://wiki.ros.org/python_2_and_3_compatible_code


Originally posted by tfoote with karma: 58457 on 2020-01-28

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by gmetts on 2020-01-30:
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.