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.
Go to
and browse to our workspace folder~/ros2_tutorial_workspace
Right-click the folder
install
and choose . Do the same forbuild
andlog
Your project view should look like so
Running a Node from PyCharm
With the project correctly configured, you can
move to
.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.
right click sample_python_node.py and choose
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
Close PyCharm.
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.
Re-open PyCharm.
pycharm_ros2