When to use Python vs Cpp with ROS

You’re programming with ROS, or are just getting started, and you wonder: should you use Python vs Cpp with ROS?

Of course… It depends. But in this post I’ll try to go beyond this “it depends” answer, to give you some points on when to you use one language or the other.

To this question, I see a 3 step question:

  • Python vs C++
  • Then Python vs C++ in robotics
  • Then Python vs C++ with ROS

I won’t answer the “Python vs Cpp” question. You can already find many debates on the Internet about this. Though some parts of the answer to that question will be useful to this post.

For the “Python vs C++ in robotics” question, please read this guide first, where I give some answers.

Yes you can use both Python and Cpp with ROS

First good news: ROS is language agnostic. It means that the underlying communication features to talk between nodes do not rely on a language, but on a lower layer instead. You can perfectly write a node in Cpp which sends data to another node in Python.


You are learning ROS?

Check out ROS For Beginners and learn ROS step by step.


Thus, the question – as I stated it in the title –  is not “should you use Python or Cpp”, the question really is “as you can use both, when should you use Python and when should you use Cpp”. You can use Cpp for some nodes, and Python for some other nodes, no problem with that!

When to create a Python node

Let’s start with Python and when you should create a ROS node using Python.

Just testing or learning ROS

ROS is quite hard to learn at first. You’ll need a few days at least before you grasp the basic concepts. So, don’t make things more complicated for you to learn. If you’re not a C++ expert, or if you’re also new to programming, just go with Python for your learning journey.

You will be able to discover all ROS core concepts with Python, and you’ll remove the entry barrier of Cpp.

Of course if you already know Cpp well and prefer to learn ROS with this language, no problem at all. But don’t force yourself, focus on learning ROS with the easiest possible tools.

The same applies when you’re just testing something new with ROS. When I write some code to test a feature I don’t know, I almost always use Python, as it allows me to write code much faster and focus on the feature itself. If I need to use Cpp for the implementation later, fine, but for quick testing and prototyping, Python is the best.

With Python, you write your node, and you launch it directly. For Cpp, each time you have to compile your code. If you want to iterate fast Python will be much faster here.

No need for performance

Some people will often say that Cpp brings more performance than Python, and that’s true. But… If one part of your application doesn’t need performance at all, why should you use Cpp if you can develop faster and better with Python?

Let’s say one node of your robot is publishing the temperature of the robot, every 2 seconds. The action to publish something at 0.5Hz here doesn’t need any performance. Optimizing this code would be useless. Another example: one node is responsible for taking a command from the user at 10 Hz and forward it to the wheels so they can run in the right direction. We’re talking about 10Hz, not 200Hz. If you’ll never need 200Hz, then optimizing the code with Cpp won’t improve the overall performance of your application.

If you prefer Python for writing non critical parts, then just use it and your application will be perfectly fine!

You’re not familiar with Cpp

This point takes some arguments previously said, but is worth mentioning here.

If you’re already a Python expert, and don’t know Cpp much, then use Python as much as you can. This will save you huge amount of time.

And when developing an application, time is often more valuable than premature optimization. As ROS makes you separate your code well with packages and nodes, you will be able to easily spot the parts that you need to change to Cpp, and implement them later.

But first, make your robot move!

You only have a Python library

For that you don’t have a choice. If you find out a library that is written only in Python, you’ll have to write some Python code to use that library. For ROS specific features, most of the standard functionalities implemented in Python are also implemented in Cpp. You’ll see this scenario mostly from libraries developed from the community – often available on GitHub.

If the library is packaged as a ROS package, with a ROS interface (topics, services, actions, etc), you might be able to use the library just with the ROS communication interfaces. In this case you can use any language you want.

When to create a Cpp node

Time to switch to the Cpp side!

You’ve just seen some arguments in favor of using Python when developing a ROS node.  Let’s see when you should create a Cpp node.

Performance – though you won’t get real time with it because of Ubuntu

Performance is often the number one factor when deciding whether you should use Cpp or not. Sometimes, you do really need performance, and no other language will be able to meet your requirements. So you need to use Cpp (or C, or assembler, but let’s not go down to binary code too soon).

Cpp is especially useful when you have loop threads that run quite fast (a few hundred Hz to a few thousand) and perform some heavy computation.

For example, a loop that gives an appropriate command at a motor – at let’s say 200 Hz – while computing motion planning between each command, may have some performance issues if written in Python.

Note that if you can reach high degrees of performances with Cpp, you might not be able to respect tight real-time constraints if you have some. The main reason is because ROS needs Ubuntu OS to run, and Ubuntu is not a real-time OS. Thus, you can’t build a real-time application if the foundation is not already real-time. (Also, many ROS parts where not developed with real-time constraints as a requirement)

Typed language

Python is well known for letting you write whatever you want, assuming that you know what you’re doing, and then… crashing at runtime, because finally you didn’t really know what you were doing.

This advantage is basically the advantage that all typed languages have over non-typed languages. It’s also a question of preference.

With a non typed language, you’ll have to write more tests as early as you can, if you don’t want to lose control after your application starts to really grow.

It’s not to say that you shouldn’t write tests for your Cpp code – you should, and everyone should, but in reality it’s not often done. Using a strong typed language here will probably make your ROS nodes stronger and more scalable for the future.

You’re already good at Cpp

If you’re already a Cpp rock-star, and can write Cpp code twice as fast as Python code, then go with Cpp, unless you have to use Python for a given library.

If using Cpp from the beginning saves you time and improves the performance of your application without any additional effort, then your choice is quite easy to make!

Only have a Cpp library

Sometimes, just as for Python, you’ll have to use a ROS library that only has a Cpp interface.

For example, the ros_control library, very useful for providing an interface to your hardware, and control it with various already implemented controllers (position, velocity, torque, etc), is only available for Cpp. And it would be a shame not to use it if you need to control hardware components such as servomotors.

This case – a Cpp only library – will be more common. Basically ROS is fully developed in Cpp, and most of the Python ROS functionalities you can find, in addition to the core features, are just Python bindings over a Cpp implementation.

The famous Moveit library, used for motion planning, has both a Cpp and a Python interface, but it’s only implemented in Cpp. In this case, while you can use both languages, you might also have some additional functionalities only for Cpp.

Conclusion: Python vs Cpp with ROS

As you saw in this post, depending on what you want to do, the time you have, the skills you have, one language might be better than the other.

There is not a universal answer to this question, but I tried to give you as much info as I could to avoid a typical “it depends” answer.

Mostly you can follow this “rule”: if you don’t need performance, and you’re not already good at Cpp, then choose Python, and most of the time you’ll be fine with it.

Did you find this tutorial useful?

Do you want to learn how to program with ROS?

If yes, this course is for you:

ROS For Beginners - A Step By Step Course

>> ROS For Beginners - A Step By Step Course <<