10

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.

Sanat
  • 133
  • 1
  • 1
  • 9

4 Answers4

7

This is for those who have installed OpenCV on Anaconda and VSCode can't suggest cv2 functions.

python.autoComplete.extraPaths didn't work for me. so according to this answer :

  1. open VSCode
  2. ctrl + shift + p
  3. type : Python: Create Terminal
  4. type python -m pip install --upgrade opencv-python in the new terminal
Pouria
  • 95
  • 1
  • 7
3

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" ]
Arun Soorya
  • 447
  • 2
  • 9
2

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"
]}
Karthik S
  • 21
  • 1
1

@Karthik S was very helpful but I still couldn't make the intellisense work; adding

"python.jediEnabled": false, 

Did the trick for me;

To sum up my settings.json file looks something like this:

{
"python.analysis.memory.keepLibraryAst": true,
"python.defaultInterpreterPath": "/usr/bin/python3",
"python.jediEnabled": false, 
"python.autoComplete.extraPaths": [
    
    "/usr/local/lib/python3.8/dist-packages/cv2/python-3.8"
],
"python.languageServer": "Microsoft"

}

Hope it solves Your problem. :)