How to import ros to PyCharm

Question:

I am developing a robot using the ros framework. As ide, I use PyCharm. But I can’t import ros into it. On the ros site there is the article about ide http://wiki.ros.org/IDEs. There is information about using ros with pycharm. I have to modify the .desktop file, but I installed PyCharm using a snap from the software center. Where can I find the .desktop file for snap applications? Can there be another method to import ros into PyCharm?

Edit:
@hug Yes, I launched this command here is the result

/snap/pycharm-community/103/meta/gui/pycharm-community.desktop
/snap/pycharm-community/103/snap/gui/pycharm-community.desktop
/var/lib/snapd/desktop/applications/pycharm-community_pycharm-community.desktop
/var/tmp/pycharm-community.desktop.swp

I think that the .swp file has remained since the last reinstallation of PyCharm. I reinstalled when I tried to do it myself and realized that I had broken the program.

Asked By: Dines

||

Answers:

If you want to use from the auto-suggestion with ROS packages in Pycharm IDE, you could do the following instructions:

  1. File > Settings (or Ctrl+Alt+s as shortcut)> Project: > Project interpreter.

  2. In the project interpreter dropdown list, you can specify ROS Python interpreter by selecting the appropriate from the list.


[NOTE]:

You could add a virtual environment with the above instructions, then you should add ROS distpackages (roslib) on it with this instruction.

ROS distpackages path that you need: /opt/ros/kinetic/lib/python2.7/distpackages

If anyone else is struggling with getting PyCharm intellisense and autocomplete to work with your own ros python packages, this answer was very illuminating.

Basically PyCharm does not dynamically interpret the devel/ space __init__.py for your packages, so at runtime the packages are available, but no intellisense in PyCharm. Changing the Project Structure settings to add your <package>/src as a Source folder solved the issue for me. Bit of a pain, but hey, intellisense is back!

Answered By: logan

Follow these video instructions for setting up pycharm with ROS.

https://www.youtube.com/watch?v=lTew9mbXrAs

Debugging, code completion, imports, etc all behave correctly. You can do all your python ROS development entirely in pycharm.

In summary:

  • Create a virtual environment virtualenv venv --system-site-packages
  • Source your virtualenv and your ROS workspace and launch pycharm from this (sourced) terminal. (enable Shell Scripts in the JetBrains Toolbox to allow command-line pycharm)
  • Under the project structure you will "Add Content Root":
  • Keep [ros_workspace]/src (which should already be there). Mark all the the src folders containing python code as "Sources".
  • add /opt/ros/noetic/lib/python3/dist-packages
  • add [ros_workspace]/devel/lib/python3/dist-packages. Exclude any packages in [ros_workspace]/devel/lib/python3/dist-packages that contain source code (e.g. are not just message packages). If you have packages that contain both source and messages, you will need to separate these for pycharm to resolve imports correctly in the editor

More details and explanation in the video

Answered By: Brad Saund
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.