How to setup ROS2 in PyCharm

image_pdfimage_print

Basic Usage of ROS2 with PyCharm

To access code completion and syntax error handling (debugging doesn’t work in PyCharm directly) in PyCharm for ROS2 the paths have to be linked in your project structure. To do so the following steps have to be performed:

Go to Settings -> Project: <Your-Project> -> Project Structure and Click on Add Content Root.
Now add the two directories: /opt/ros/<your-ros-distro>/lib/<python-version>/site-packages and /opt/ros/<your-ros-distro>/local/lib/<python-version>/dist-packages and set them to Resources

Now the code completion and syntax error handling works for ROS2.

Custom Messages & Custom Packages

To resolve import errors and add code completion in PyCharm for ROS2 projects you have to further set some directories as source roots.

First for custom messages (now called interfaces) build your project once with colcon build. Then go to install -> <name-of-msg-package> -> local -> lib -> <python-version> -> dist-packages and mark this folder as Source with the right click drop down menue. 

For custom packages you go to install -> <name-of-ros2-package> -> lib -> <python-version> -> site-packages and mark this as Source with the right click drop down menue.

Don’t forget the build your project with colcon build if you change your custom packages/modules so the code is updated in the install directory.

Custom Packages and setup.py

To use custom packages in your ROS2 nodes you need to link them in your setup.py file of your ROS2 package. To do so all packages need to have a __init__.py file depending on your package this file is in general empty. Then you open the setup.py file and add all your packages to the list “packages”. The list should look like this: 

packages = [package_name, package_name + ‘/Your_Package’, package_name+’/Your_2nd_Package’],

Use colcon build to update your install directory.

2 thoughts on “How to setup ROS2 in PyCharm

  1. What steps should be followed to incorporate custom messages (interfaces) into a project using colcon build, including marking a specific folder as a Source by navigating to the appropriate directory within the install directory?

  2. Hi,

    I had a question about your recommendation for adding folders to content root. My installation of ros2 doesnt have a /local folder within my distro and I’m not too sure if that will cause many issues or not. Do you have any advice? I am using ROS2 Foxy and Ubuntu 20.04

    Cheers
    Adhi

Leave a Reply

Your email address will not be published. Required fields are marked *