Using PyCharm Code Completion with a ROS2-Docker Environment

I've been trying to set up an environment for running ROS2 inside a docker container. I was able to successfully get some basic ROS2 code running inside the container, however, I am unable to get code completion to work for anything other than python packages I install via the Dockerfile.

According to the ROS2 documentation for IDEs ( A similar problem is solved in linux by launching PyCharm from a shell that has sourced the ROS2 source.bash (source /opt/ros/<distro>/setup.bash).

I would like to know if there is a way to have PyCharm use the docker shell environment for code completion or perhaps a way to source the source.bash file from the docker container? Any advice is appreciated.

Comment actions Permalink

I haven't tried this in ROS2 yet, but in ROS, for code completion, you don't necessarily need to source a set-up file as long as the python interpreter inside the container has the ROS-installed python packages in its path.  The easiest way I've found to accomplish this is to symlink the ROS packages from your interpreter's default site-packages location. The exact paths will differ depending on your particular environment, but the command should look something like this:
```ln -s /opt/ros/<ROSDISTRO>/lib/pythonX.X/dist-packages ~/local/lib/pythonX.X/site-packages/ros-packages```
then you need to add this symlink to the list of packages that should be loaded into the path by creating a .pth file in the site-packages directory:
```echo "ros-packages" > ~/local/lib/pythonX.X/site-packages/ros.pth```
This will allow the remote interpreter to build the skeletons for all of the rospy modules installed by your package manager, but it won't give you access to any new modules you are building in your catkin workspaces. You can do a similar thing for your local development environment by copying your workspace into Docker and doing a a full `catkin_make install` and symlinking to the workspace's install directory, but this still mandates you rebuild your image every time you want to propagate the changes in your workspace into the remote interpreter. I'm still looking for better ways to do this latter part.

Comment actions Permalink

After more investigation, I believe what I need is for the docker-compose remote interpreter to use the docker entrypoint for code completion? Not sure if that is possible or not.


Please sign in to leave a comment.