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

ROS Python Module - Autocompletion doesn't work when self-compiled

asked 2020-01-10 01:49:33 -0500

TobiMiller gravatar image

Hey,

i have written a package with an python module in it. I can import it in my other packages but VSCode cannot autocomplete functions.

from <ros_python_module> import <class>

when i click on <ros_python_module>, it goes into an autogenerated "_ _init__.py" inside of my devel folder. There is no code from me.

# -*- coding: utf-8 -*-
# generated from catkin/cmake/template/__init__.py.in
# keep symbol table as clean as possible by deleting all unnecessary symbols

Is it possible that catkin build makes a copy of the files and moves them into the devel folder?

Example: moveit_commander -> from package manager i can autocomplete inside vscode --> compiling by myself and it doesnt autocomplete

edit retag flag offensive close merge delete

Comments

VSCode needs to know where the actual code is. You probably need to edit the configuration file of VSCode or use a plugin that knows where to look for python code in ROS packages and does this for you. Copying the files to where VSCode is now looking doesn't sound like a good idea.

davekroezen gravatar image davekroezen  ( 2020-01-10 09:07:46 -0500 )edit

VSCode knows the PYTHONPATH but how i said, catkin compiles an auto generated init py which is not introspecteable :/

TobiMiller gravatar image TobiMiller  ( 2020-01-11 04:37:02 -0500 )edit

Hi Tobi, I'm having the same problem: even though import works, I can't get autocomplete on my IDEs (PyCharm or VSCode) for my custom source modules. I'm wondering if you found a solution for this?

vbs gravatar image vbs  ( 2020-09-30 13:22:05 -0500 )edit

@vbs you have to add the folder to your vscode settings.json. "python.autoComplete.extraPaths"

TobiMiller gravatar image TobiMiller  ( 2020-10-01 02:03:25 -0500 )edit

Is that the package folder? I added it like this but made no difference: "python.autoComplete.extraPaths": [ "/home/vbs/catkin_ws/src/test_python_import/src/test_python_import" ]

vbs gravatar image vbs  ( 2020-10-01 05:20:40 -0500 )edit

extraPaths should point to the folder that contains a folder with __init__.py file. In my case it was:

"python.analysis.extraPaths": [
    "/opt/ros/noetic/lib/python3/dist-packages",
    "./src/<pkg_name>/src"
  ]
dcardenasn gravatar image dcardenasn  ( 2021-09-28 14:00:29 -0500 )edit

3 Answers

Sort by » oldest newest most voted
0

answered 2022-03-14 07:06:14 -0500

In VSCode adding the src file paths of the module worked for me instantly.

Open

Preferences->Python->Analysis:Extra Paths

and then add

/home/<user>/ros_ws/<ros_pkg>/src
edit flag offensive delete link more
1

answered 2021-11-12 18:51:24 -0500

papalotis gravatar image

I have tried all the mentioned solutions but none of them worked for me, here is what I ended up with.

My goal is to get Pylance (the better vscode language server) to recognize my ros packages and not default to the devel/... generated file. For this the only important thing is for the package directory (catkin_ws/src/python_package/src/) to be present after the generated (catkin_ws/devel/lib/python3/dist-packages) one in the PYTHONPATH global variable. For some reason pylance (and pyright) always pick the last valid import path when analysing an import. This way custom package code points to the implementation and generated msgs still point to the generated code.

TL;DR

first source devel/setup.bash then add src/python_package/src to pythonpath

edit flag offensive delete link more
1

answered 2020-03-17 07:53:29 -0500

tonyp gravatar image

You can create a .env file with a PYTHONPATH variable with all the source paths in it, then reference this file from the python.envFile setting in VS code. This path will only be used by the python extension tools and not by the terminal. In this way when running from the terminal you are still running in a "deployed" situation from devel but the tools can still find the source files. It isn't the prettiest but it is a work around.

https://stackoverflow.com/questions/5... https://code.visualstudio.com/docs/py...

edit flag offensive delete link more

Question Tools

4 followers

Stats

Asked: 2020-01-10 01:49:33 -0500

Seen: 1,953 times

Last updated: Jan 10 '20