microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.72k stars 766 forks source link

Autocomplete suggestions not working for OpenCV built from source #1609

Closed mosdeo closed 2 years ago

mosdeo commented 3 years ago

Environment data

VS Code version: 1.58.2 Extension version (available under the Extensions sidebar): v2021.7.5 OS: Darwin arm64 20.6.0 Python version (& distribution if applicable, e.g. Anaconda): 3.9.2 Type of virtual environment used (N/A | venv | virtualenv | conda | ...): conda Relevant/affected Python packages and their versions: OpenCV 4.5.3 Value of the python.languageServer setting: Pylance

OpenCV 4.5.3 built from source and non-free packages, installed in:

/Library/OpenCV/opencv-4.5.3/build/lib/python3
/usr/local/lib/python3.9/site-packages/cv2/python-3.9

Both paths is added to "python.analysis.extraPaths"

"python.analysis.extraPaths": [
        "/Library/OpenCV/opencv-4.5.3/build/lib/python3",
        "/usr/local/lib/python3.9/site-packages/cv2/python-3.9"
    ],

Expected behavior

Autocomplete suggestions for module cv2 are expected. Autocomplete works in python.languageServer==Microsoft , so that proves nothing wrong with my installation of OpenCV.

Actual behavior

Autocomplete suggestions working fine with the Python default method.

image

But, not working with the OpenCV built from source

image

erictraut commented 3 years ago

If I rename the bundled/stubs/cv2-stubs directory to bundled/stubs/cv2, it works fine.

jakebailey commented 3 years ago

Aha, just that folder. The way we do it right now is weird as it is, we should really just be sticking our bundled stubs in as another search path with the other third party libs. I'll take a look.

jakebailey commented 3 years ago

Renaming it kills the partial-ness, as you have to have it be a -stubs for it to be a partial stub, though. How are you managing to reproduce this? We thought we could reproduce it and fixed it (hence, closing this once), but apparently the setup still differs.

erictraut commented 3 years ago

I installed the latest version of pylance, then typed:

import cv2
cv2.imread

Pylance reported that it couldn't resolve the import even though the bundled stub was present.

I just realized that I don't have cv2 installed in my virtual environment. (My venv is Python 3.9, and apparently cv2 isn't compatible with it.) My expectation was that pylance would use the bundled cv2 partial stub even though I don't have the cv2 library installed, but perhaps that's a bad assumption? Stub libraries (partial or otherwise) shouldn't require the associated library to be installed.

jakebailey commented 3 years ago

Our cv2 stub is a partial stub, and can only apply if the library is actually installed, as it's only stubbing out a subset of the modules (via a FS overlay). This is probably the only partial stub that's of the top level module, though, and not some deeply nested compiled module, so I can see why it would seem like it would resolve, but that's not how this particular stub is designed to function.

I think the user above is in a situation where the library is installed (via some local build), but it's still not working. @bschnurr tried to emulate this buy doing local builds, package installs in Ubuntu, etc, which both found the module naming mismatch plus the dist-packages thing, neither of which seemed to matter (even though the former should have really worked).

I tried to go to https://zhuanlan.zhihu.com/p/394141304 to see if there are build instructions there, but that site doesn't load for me.

The only thing I can think of at this point is that the module is cv2.cpython-39-darwin.so, but the library we stubbed out is cv2-stubs/__init__.py, and the mismatch is causing problems. Previously we had cv2.cv2 as a native stub, as from our experience OpenCV as shipped on PyPI actually just reexports everything in an cv2/__init__.py from cv2.cv2 (with a few extra members); it doesn't actually ship to end users as a single sofile. That to me seems like the problem, and I'm not sure how to resolve it (or if this is even a valid way to build the library, if this __init__.py is important.

mosdeo commented 3 years ago

I tried to go to https://zhuanlan.zhihu.com/p/394141304 to see if there are build instructions there, but that site doesn't load for me.

The same article in Github.io https://mosdeo.github.io/2021/07/25/%E5%A6%82%E4%BD%95%E5%9C%A8Apple%20Silicon%E4%B8%8A%E5%AE%89%E8%A3%9DOpenCV%E8%88%87TensorFlow%E7%B5%A6Python%E7%94%A8%EF%BC%9F/

bschnurr commented 3 years ago

when I was trying to repo i didn't use a venv with a symlink to cv2 in usr/local/lib/python3.9... I was just using usr/bin/python3/ or /bin/python3/ and they were both python3.8 image

i'll try again with a venv and symlink

heejaechang commented 2 years ago

@mosdeo is this still an issue with latest pylance release?

mosdeo commented 2 years ago

@mosdeo is this still an issue with latest pylance release?

Pylance(v2022.3.2)working better than before, but not all.

It works in the OpenCV static method:

image

But not working in the OpenCV dynamic object menber method:

image

The same dynamic object member method under Jedi:

image

Auto suggestions still not works in the OpenCV dynamic object menber method:

image

The same dynamic object member method under Jedi:

image
heejaechang commented 2 years ago

@mosdeo pylance is a static analyzer, none of dynamic behavior will be supported. for those, Jupyter is needed (assuming you ran the cell)

heejaechang commented 2 years ago

please, open new issue if there are other issues around cv2. this issue got too long with bunch of issues mixed together.