Thiwakar S
Thiwakar S

Reputation: 21

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!

Upvotes: 1

Views: 3901

Answers (4)

Lucky
Lucky

Reputation: 21

the best way is to open your IDE on the same terminal where u had sourced the setup file before.

the package is'nt recognized outside ROS, as these are installed in the site-packages folder that lies within ros2 directory.

Upvotes: 0

kostis kakkavas
kostis kakkavas

Reputation: 11

Hello the solution I found came from this site

https://cps.unileoben.ac.at/how-to-setup-ros2-in-pycharm/

in case later on it gets removed here is the text:

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: -> Project Structure and Click on Add Content Root. Now add the two directories: /opt/ros//lib//site-packages and /opt/ros//local/lib//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 -> -> local -> lib -> -> dist-packages and mark this folder as Source with the right click drop down menue.

For custom packages you go to install -> -> lib -> -> 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’],

but also after this you have to include the correct environment variables in the configuration you are about to run in pycharm. to do this open Run > Edit Configurations... and then get the current ones from your machine by running in terminal (linux) $ printenv | grep 'ros|ROS' on other operating systems you can find them similarly and get those that have either ros or ROS in the name

Upvotes: 1

sungchan1
sungchan1

Reputation: 1

I finally managed to solve the problem and wrote a blog post about it in Korean.

https://du-sungchan-24k.tistory.com/37

In summary, you need to directly inject this into your pycharm.sh script.

. /opt/ros/humble/setup.sh' 

If installed via Jetbrain Toolbox, your pycharm.sh will be located at

'/home/{USER_NAME}/.local/share/JetBrains/Toolbox/apps/{pycharm_version}/bin/pycharm.sh'. 

For Jetbrain Remote Development, pycharm.sh is in

'/home/{USER_NAME}/.cache/JetBrains/RemoteDev/dist/{cached_pycharm_version}/bin/pycharm.sh'. 

The same applies to CLion and IntelliJ."

Upvotes: 0

Thiwakar S
Thiwakar S

Reputation: 21

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?

Upvotes: 0

Related Questions