ROS1 officially supports Python 2, but there are many official libraries that are written with compatibility for Python 3. TF2 is one of them, and it may be a good idea to start using Python 3 for simple applications that make use of TF2.

What’s Required

My Environment

  • Ubuntu 16.04
  • ROS Kinetic
  • Python 2.7.12 (system)
  • Python 3.5.2 (system)
  • Python 3.6.4 (installed with pyenv)


Note: You may want to create a different catkin workspace for this. Otherwise, you’ll probably need to recompile all the packages that depend on TF2 libraries.

  1. Install pyenv following the instructions in the URL above
  2. CONFIGURE_OPTS=--enable-shared pyenv install <version> to install the latest Python 3 with the shared libraries
  3. Download geometry2 and put it in your catkin workspace
  4. Go into your catkin workspace and run pyenv local <python version you installed>
  5. Run pyenv rehash and make sure python --version displays the right version
  6. Install dependencies with: pip install catkin_pkg pyyaml empy rospkg numpy
  7. Run catkin_make -DPYTHON_EXECUTABLE=$( pyenv prefix )/bin/python3 -DPYTHON_LIBRARY=$( pyenv prefix )/lib/ -DPYTHON_VERSION=3
  8. In a Python 3 console, run import tf2_ros.

If you get the following error when running catkin_make, make sure that the executables you’re pointing to exists, and that they’re the right version.

When I forgot to specify -DPYTHON_EXECUTABLE so cmake couldn’t find the right Python interpreter,

CMake Error at /usr/share/cmake-3.5/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
  Could NOT find PythonInterp: Found unsuitable version "2.7.12", but
  required is at least "3" (found /usr/bin/python)
Call Stack (most recent call first):
  /usr/share/cmake-3.5/Modules/FindPackageHandleStandardArgs.cmake:386 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.5/Modules/FindPythonInterp.cmake:163 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  /opt/ros/kinetic/share/catkin/cmake/python.cmake:8 (find_package)
  /opt/ros/kinetic/share/catkin/cmake/all.cmake:147 (include)
  /opt/ros/kinetic/share/catkin/cmake/catkinConfig.cmake:20 (include)
  CMakeLists.txt:52 (find_package)

I got this while compiling rviz because I didn’t specify -DPYTHON_LIBRARY.

CMake Error at /usr/share/cmake-3.5/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
  Could NOT find PythonLibs: Found unsuitable version "2.7.12", but required
  is at least "3.6" (found

If you get the following error in the Python console, suspect that you’re not using the right Python version:

>>> import tf2_ros
Cannot load geometry_msgs moduleTraceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/", line 49, in <module>
    from .client import spin, myargv, init_node, \
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/", line 52, in <module>
    import roslib
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/roslib/", line 50, in <module>
    from roslib.launcher import load_manifest
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/roslib/", line 42, in <module>
    import rospkg
ImportError: No module named 'rospkg'


Python 3.6.4 (default, Jan 21 2018, 19:20:10)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rospy
>>> import tf2_ros
>>> from geometry_msgs.msg import TransformStamped
>>> rospy.init_node('sample')
>>> static_tf = TransformStamped()
>>> tf_sb = tf2_ros.StaticTransformBroadcaster()
>>> static_tf.header.stamp =
>>> static_tf.header.frame_id = 'base_link'
>>> static_tf.child_frame_id = 'laser'
>>> static_tf.transform.rotation.w = 1.0
>>> tf_sb.sendTransform(static_tf)

And that’s how you use TF with Python 3!