Working With Python 3 In ROS Kinetic Or Melodic

ROS (Robot Operating System) - Documentation -

The Robot Operating System (ROS) is a flexible framework for writing robot software. It is a collection of tools, libraries, and conventions that aim to simplify the task of creating complex and robust robot behavior across a wide variety of robotic platforms. (Ref.

ROS (upto Melodic) officially supports only python2 and NOT python3. However some libraries we use in our projects (eg. Speech Recognition using Google Cloud Speech) may require python3 to run.

If ROS needs to support python3 we may have to recompile ROS source code using python3 which is not practical.

So what we can do is to run python3 programs, separately and connect using ROS bridge. (if we use custom messages (ROS msg)

However, if we are not using any custom rosmsg and using only built-in rosmsg, we can do the following steps to run python3 codes in ROS (without using a ROS bridge.)

Install ROS (here I install Melodic)
     apt install ros-melodic-desktop-full

After installing ROS, install rospkg for python3
     apt install python3-pip python3-all-dev python3-rospkg

This will prompt to install python3-rospkg and to remove ROS packages (already installed). Select Yes for that prompt. This will remove ROS packages and we will have to re-install them.
     apt install ros-melodic-desktop-full --fix-missing

This will complete the installation part. Now comes the coding part.

Just include the following directive as the first line of your program code (file) which should be executed using python3.
     #!/usr/bin/env python3

We can now execute everything as we do normally in ROS. 
Read the documentation (link is given above) for more information on ROS. 

Post a Comment