ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Configure PyCharm to make the linting work properly

asked 2018-09-24 07:28:56 -0500

Hug gravatar image

Hi everyone,

I'm trying to use PyCharm with a Python ROS node. The linting displays the message No module named [...] for all the imports related to ROS (rospy, std_msgs, tf, ...). It seems that I selected the right version of Python, but rospy and the others don't appear:

image description

I think I have to tell PyCharm to look in /opt/ros/kinetic/share, but I don't know how to do that.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2018-09-24 09:36:10 -0500

updated 2018-10-01 11:05:21 -0500

From terminal:

$ source /opt/ros/kinetic/setup.bash
$ pycharm-community

I went ahead and added source /opt/ros/kinetic/setup.bash to the bottom of ~/.bashrc.


Edit: To run PyCharm via application shortcut...

  1. Find PyCharm .desktop file:
    $ locate .desktop | grep pycharm
  2. Open the PyCharm .desktop file:
    $ sudo -H gedit /PATH_TO_PYCHARM_DESKTOP_FILE/PYCHARM_DESKTOP_FILE.desktop
  3. Add bash -i -c to the Exec=... line within the desktop file.
  4. Save and exit.

Example (Ubuntu 16.04, PyCharm Community 2018.2.4):

$ locate .desktop | grep pycharm  
$ sudo -H gedit /var/lib/snapd/desktop/applications/pycharm-community_pycharm-community.desktop

pycharm-community_pycharm-community.desktop

original:

[Desktop Entry]
Version=1.0
Type=Application
Name=PyCharm Community Edition
Icon=/snap/pycharm-community/85/bin/pycharm.png
Exec=env BAMF_DESKTOP_FILE_HINT=/var/lib/snapd/desktop/applications/pycharm-community_pycharm-community.desktop /snap/bin/pycharm-community %f
Comment=Python IDE for Professional Developers
Categories=Development;IDE;
Terminal=false
StartupWMClass=jetbrains-pycharm-ce

edited:

[Desktop Entry]
Version=1.0
Type=Application
Name=PyCharm Community Edition
Icon=/snap/pycharm-community/85/bin/pycharm.png
Exec=env BAMF_DESKTOP_FILE_HINT=/var/lib/snapd/desktop/applications/pycharm-community_pycharm-community.desktop bash -i -c /snap/bin/pycharm-community %f
Comment=Python IDE for Professional Developers
Categories=Development;IDE;
Terminal=false
StartupWMClass=jetbrains-pycharm-ce

References:
- https://answers.ros.org/question/2043...
- https://askubuntu.com/a/913536
- https://superuser.com/questions/20267...

edit flag offensive delete link more

Comments

Thanks. In fact, I had already sourced /opt/ros/kinetic/setup.bash and ~/catkin_ws/devel/setup.bash in ~/.bashrc. So I just needed to run PyCharm from the console. Your solution is surely more elegant than mine. The only drawback is that I can't run PyCharm with the shortcut that way.

Hug gravatar image Hug  ( 2018-09-24 09:51:26 -0500 )edit

I edited answer with method that should allow you to run PyCharm via shortcut.

josephcoombe gravatar image josephcoombe  ( 2018-09-24 12:04:56 -0500 )edit

Thanks! It works well.

Hug gravatar image Hug  ( 2018-09-25 02:46:59 -0500 )edit

It works for me, ubuntu 18.04 and ros melodic.

sxin gravatar image sxin  ( 2018-11-28 08:32:09 -0500 )edit

Hello, it did work for ubuntu 18.04 and melodic, for a while --- but I just reinstalled a new computer, and it seems that adding bash -i -c ... strangely works the first time, and after that it will still run, but everything will be in background. ps aux | grep pycharm will show a process, but no output on the screen.

Best regards, C.

wintermute gravatar image wintermute  ( 2019-11-30 17:43:13 -0500 )edit
0

answered 2018-09-24 09:28:26 -0500

Hug gravatar image

updated 2018-09-24 09:34:55 -0500

It seems that PyCharm Community Edition has no built-in way to add external dependencies. However, I found a workaround here.

So I created a symbolic link to the folder /opt/ros/kinetic/lib/python2.7/dist-packages/ (which contains the Python modules associated to the ROS packages installed on my computer) in the root of my ROS package:

ln -s /opt/ros/kinetic/lib/python2.7/dist-packages/ ros_kinetic_python

Then, in PyCharm, in File -> Parameters... -> Projet: <project_name> -> Project Structure, I right-clicked on ros_kinetic_python (my link) and chose Sources. Then, I clicked Apply and closed the window, and now it PyCharm finds the modules!

Note that you can do the same for the Python modules in your catkin workspace, just create a link to <you_catkin_workspace_path>/devel/lib/python2.7/dist-packages/.

edit flag offensive delete link more

Comments

Thanks! It worked for me. I'm new to PyCharm, that was a very nice workaround to get things working

CaipirUltron gravatar image CaipirUltron  ( 2019-12-02 12:21:52 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-09-24 07:28:56 -0500

Seen: 1,968 times

Last updated: Oct 01 '18