Using PyCharm for ROS2 sources

With PyCharm opened as instructed in Editing Python source (with PyCharm), here are a few tips to make your life easier.

  1. Go to File ‣ Open… and browse to our workspace folder ~/ros2_tutorial_workspace

  2. Right-click the folder install and choose Mark Directory as ‣ Excluded. Do the same for build and log

Your project view should look like so

_images/pycharm_project.png

Running a Node from PyCharm

With the project correctly configured, you can

  1. move to src ‣ python_package_with_a_node ‣ python_package_with_a_node.

  2. double (left) click sample_python_node.py to open the source code, showing the contents of the Node. It is minimal to the point that it doesn’t have anything related to ROS at all.

  1. right click sample_python_node.py and choose Run sample_python_node

It will output in PyCharm’s console

Hi from python_package_with_a_node.

Note

You should extensively use the Debugger in PyCharm when developing code. If you’re still adding print functions to figure out what is wrong with your code, now is the opportunity you always needed to stop doing that and join the adult table.

Note

You can read more about debugging with PyCharm at the official documentation.

What to do when PyCharm does not find the dependencies

Note

This section is meant to help you troubleshoot if this ever happens to you. It can be safely skipped if you’re following the tutorial for the first time.

Note

There might be ways to adjust the settings of PyCharm or other IDEs to save us from the trouble of having to do this. Nonetheless, this is the one-size-fits-most solution, which should work for all past and future versions of PyCharm.

If you have ruled out all issues related to your own code, it might be the case that the terminal in which you initially ran PyCharm is unaware of certain changes to your ROS2 workspace.

To be sure that the current PyCharm session is updated without changes to any settings, do

  1. Close PyCharm.

  2. Build and source the ROS2 workspace.

    cd ~/ros2_tutorial_workspace
    colcon build
    source install/setup.bash
    

    Note

    If you don’t remember why we’re building with these commands, see Always source after you build.

  3. Re-open PyCharm.

    pycharm_ros2