pythonpycharmros2

Python package is installed but it doesn't get detected in a IDE #ROS2


I'm here trying to follow a tutorial on ROS2 from a Udemy course and as by the tutorial, I created a bare minimal node and the main package RCLPY is not recognised in the IDE. I tried this with Pycharm. But when the code is installed using colcon build the code works as intended.

RCLPY not recognised in Pycharm

since the package is not recognised, the auto complete is also not working.

I'm using ROS2 humble distro on ubuntu 22.04LTS.

RCLPY listed by using pip

RCLPY package not found in Pycharm

Code running in Terminal after building in COLCON BUILD

Error regarding RCLPY in pycharm

here is the snippet of the XML file from ROS2 workspace package.xml

I don't know how to proceed from here. Thank you for your help!


Solution

  • So I myself found a answer just to import the rclpy module inside pycharm. The real problem here is that the interpreter in pycharm doesn't look for the ros2 modules automatically so we need to add them manually. Here are the steps to add the paths of module to make the interpreter look for it.

    step 1 : Go to file > settings > python Interpreter > show all

    step 2 : click on the icon with small size folders next to the filter icon 'the icon next to filter icon'

    step 3 : include the paths mentioned below using the plus symbol.

    /opt/ros/<distro>/bin
    /opt/ros/<distro>/local/lib/python3.10/dist-packages
    /opt/ros/<distro>/lib/python3.10/site-packages
    /opt/ros/<distro>/lib
    

    Reference Image for Paths

    my distro is ROS2 HUMBLE, the python version depends on what python version that you are using.

    step 4 : press ok and restart the Pycharm IDE

    I'm still facing a error ImportError: librcl_action.so: cannot open shared object file: No such file or directory

    will update this thread when I found a solution. Also refer this github page for further information github thread

    UPDATE REGARDING THAT ERROR MENTIONED ABOVE:

    As said before I was having problems with IDEs not recognising the rclpy library of ROS2. As by the comment yesterday I mentioned how I solved the problem by adding the interpreter paths in Pycharm IDE. The Pycharm IDE tries to run the file inside the IDE or something like that and received the same error as the title of this thread. Today I tried the code with visual studio code, surprisingly VS code recognised that rclpy library and showed all the auto complete and other things of that library. Since VS code runs every file in terminal inside the IDE, so the code works without any issues. I think this might fix your problem with that error?