Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode autocomplete not working for OpenCV installed from source

I've only found one other question asking exactly this with no answer, so I'm asking here.

I am running Ubuntu 18.04, VSCode latest version.

I have installed OpenCV 3.4.9 from source to /usr/local

When I import cv2 and then try to type "cv2.", VSCode is unable to autocomplete. The only suggestions it makes are "bootstrap" and "os". I have no problem with autocomplete with any other module like numpy or rospy, or even when OpenCV is installed from pip. It seems the issue is only when OpenCV is installed from source.

I have tried both the language server as well as Jedi. I have also tried various linters.

Any help is appreciated.

like image 450
Sanat Avatar asked Feb 27 '20 07:02

Sanat


People also ask

Why is my autocomplete not working in VS Code?

Why is VS Code suggestions not working? If you're coding in JavaScript or TypeScript and finds that VSCode IntelliSense does work but does not behave properly, it's likely that you've selected the wrong language mode. TypeScript and JavaScript share the same language service, so you need to select the right language.

How do I trigger autocomplete in VS Code?

You can trigger IntelliSense in any editor window by typing Ctrl+Space or by typing a trigger character (such as the dot character (.)


2 Answers

I have encountered the same problem. Hope this helps

Its because the package is not installed in the usual location but in a custom location. This problem could be resolved by some configuration changes. Configure the settings.json to include the custom location for autocompletion to work. Add path to python.autoComplete.extraPaths

STEP 1:Identify the location of the custom library/module.

STEP 2: Make the necessary changes in the User Settings or Workspace Settings file.

Here’s a sample entry in the User Settings (Mention the exact location of the module)

    "C:/Program Files (x86)/---/---",
    "C:/Program Files (x86)/---/---/lib" ]
like image 176
Arun Soorya Avatar answered Oct 19 '22 01:10

Arun Soorya


In case of Ubuntu 20.04 and manual installation of OpenCV4, the python library path to be added to extraPaths is "/usr/local/lib/python3.8/dist-packages/cv2/python-3.8". Assuming -D CMAKE_INSTALL_PREFIX=/usr/local was used during cmake

Ex: "settings.json" (VScode)

{
"python.analysis.memory.keepLibraryAst": true,
"python.defaultInterpreterPath": "/usr/bin/python3",
"python.autoComplete.extraPaths": [
 "/usr/local/lib/python3.8/dist-packages/cv2/python-3.8"
]}
like image 43
Karthik S Avatar answered Oct 19 '22 01:10

Karthik S