(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Identify dependencies using Python

Description: The first step to determining if your packages support Python 3 is to determine which of your dependencies depend on Python.

Tutorial Level: INTERMEDIATE

Next Tutorial: Source code changes to support Python 3

This tutorial is part of a series about transitioning a ROS 1 package to Python 3.

Identify dependencies using Python

While you can make changes to your package to support Python 3 at any time, your package's dependencies must already support Python 3 before you can test that your changes work. The first step to determining if your packages support Python 3 is to determine which of your dependencies depend on Python.

Identify rosdep keys and ROS packages

Dependencies will fall into two categories: rosdep keys and ROS packages. You'll need to know which category a dependency belongs to in order to check if it depends on Python.

Open your package's package.xml and look at all the dependencies it lists. For each dependency, you can determine if it is a rosdep key or a ROS package by trying to resolve it using rosdep. If it is a ROS package, then on your operating system it will resolve to an apt package beginning with ros-<rostdistro>-. If it is a rosdep key, it will resolve to something else.

Reference:

Example

kdl_parser_py is a ROS package

$ rosdep resolve --rosdistro=melodic kdl_parser_py
#apt
ros-melodic-kdl-parser-py

python-numpy is a rosdep key

$ rosdep resolve --rosdistro=melodic python-numpy
#apt
python-numpy

Write down two lists:

  1. dependencies that are rosdep keys
  2. dependencies that are ROS packages

Identify rosdep keys depending on Python 2

Now that you have a list of rosdep keys, it is time to determine if they depend on Python 2. There is a tool to do this called py3-ready. Install it using the instructions in its README. <!-- TODO add install instructions to it's readme -->

For each of your rosdep keys, use py3-ready check-rosdep <rosdep key> to check if it depends on Python 2.

$ py3-ready check-rosdep python-sip
rosdep key python-sip depends on python

It might be ok that some rosdep keys depend on Python 2. For example, the rosdep key boost depends on Python 2, but that is only an issue if you are using Boost Python to create a Python 2 module.

Make a list of all rosdep keys that depend on Python 2 and install Python modules that your package uses. All other rosdep keys can be ignored.

Identifying ROS packages depending on Python 2

py3-ready can also tell which ROS packages depend on Python 2. However, the important information is whether your package uses any Python modules from it. Check your package's Python code for imports of Python modules with the same name as the ROS package.

Your package might be using Python modules from dependencies of your dependencies. You can investigate this by using py3-ready check-package --quiet --dot <your package name>. This will output a graph of dependencies leading to Python 2 in dot format. Use a tool like WebGraphviz to visualize it.

Make a list of all ROS packages through which your package gets Python 2 modules directly or transitively.

Finding Python 3 versions of dependencies

Your package's dependencies will need to support Python 3 before your package can support it. This means finding replacements for your rosdep keys that support Python 3, and making sure ROS packages you depend on also support it.

Finding Python 3 equivalent rosdep keys

If you depend on rosdep keys which depend on Python 2, your package will need to use a different rosdep key in Noetic. For most cases this means finding an equivalent rosdep key that supports Python 3. For example, an equivalent to python-numpy is python3-numpy.

Rosdep keys tend to match Debian package names. Most of the time this means your current rosdep key begins with python-, and the replacement will begin with python3-, but this is not always the case. If your key starts with python-, then first try finding a key in python.yaml beginning with python3-. Otherwise, try finding keys with similar names.

If you cannot find an equivalent key, then it likely hasn't been created yet. Create a new key following the instructions at ros/rosdistro/CONTRIBUTING.md. Here is an example of a pull request adding a Python 3 rosdep key.

Determining if upstream ROS packages support Python 3

All ROS packages your package depends on will need to support Python 3 before you can test your package. Currently the Noetic build farm is not available, and is not expected to be until Ubuntu releases the first packages for their F release.

In the meantime, a source entry in the noetic/distribution.yaml is the recommended way to signal that a repository has started transitioning to Noetic. It does not mean the packages are 100% working; instead, it means it might be far enough along for your package to begin testing your package with it. All packages planning to release to Noetic should be added to this file, not just the ones using Python.

If there is no source entry for that package, then check upstream repository for any open issues about Python 3. If they have started making changes, then consider asking the maintainer to add a source entry for Noetic, or add one yourself. If they have not begun the transition to Python 3, then consider helping by opening issues and pull requests.

Next Tutorial: Source code changes to support Python 3

Wiki: UsingPython3/IdentifyDependencies (last edited 2021-04-24 00:41:15 by IsaacSaito)