Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No intellisense for cv2 (opencv) #138

Closed
deTrident opened this issue Jul 19, 2020 · 32 comments
Closed

No intellisense for cv2 (opencv) #138

deTrident opened this issue Jul 19, 2020 · 32 comments
Assignees
Labels
needs stub waiting for user response Requires more information from user

Comments

@deTrident
Copy link

 import cv2

cv2.imread("XXX")

Prompt there is a bug,cannot reach the method of imread()

Replace back to python language server, everything is ok

@judej judej self-assigned this Jul 20, 2020
@bschnurr
Copy link
Member

opencv/opencv#14590

@jakebailey
Copy link
Member

Similarly to #103: The cv2 appears to be a compiled module. Pylance currently only supports compiled modules if they are accompanied by type stubs, which cv2 does not include.

We currently don't have a scraper that will create type stubs for compiled modules. Therefore, for now, a fix would be for cv2 to ship a type stub to describe their code (that linked issue).

@jakebailey jakebailey changed the title No intellisense for cv2 and pyGdal No intellisense for cv2 (opencv) Jul 23, 2020
@manipopopo
Copy link

manipopopo commented Jul 31, 2020

Maybe we can manually use stubgen to generate .pyi as a workaround.

  1. pip install mypy
  2. stubgen -m cv2 -o {CV2_PACKAGE_FOLDER}

@sunshineharry
Copy link

Well, does the Microsoft have the plan to fix it?

@WannabeMaker
Copy link

WannabeMaker commented Aug 3, 2020

I have the same issue as deTrident, but unfortunately reverting back to Jedi did not solve my problem.
One piece of information that I have found confusing is that the CV2 autocomplete work perfectly well under the python interactive within vs-code.
I would like to say that for other librairies pylance works like a charm and I like it quite a bit.
If you need more information from me please let me know and will do what I can to get it to you.

EDIT:
@manipopopo solution worked for me. as a temporary fix

@EhsanKia
Copy link

EhsanKia commented Aug 4, 2020

I'm curious, how did the old language server do it (the ms-python.python package`)? That one handled cv2 perfectly and worked as expected. Did that one have a scraper, which has not yet been added to PyLance? Would it be able to default back to a type-less version when type stubs are not available? Having type-less autocomplete is better than having nothing at all.

@jakebailey
Copy link
Member

It did have a scraper which ran on libraries and only libraries, but doing so is iffy for a multitude of reasons, running arbitrary code being one of them. Note that Pylance (being a type checker under the hood) doesn't play extremely nicely with stubs that come out of the scraper, as the scraper almost never gets any types (and often signatures) correct. MPLS was not a type checker and internally had many assumptions which would have never made it a type checker, so adding more inaccuracy to the mix wasn't fatal.

We haven't ruled out a scraper, but there are other paths we can take that we think will produce a better experience than trying to add it back at this stage.

@lwyj123
Copy link

lwyj123 commented Sep 27, 2020

Any updates? I saw it was added 'compiled' tag

@fffzzy
Copy link

fffzzy commented Nov 3, 2020

This problem seems related with Pylance server. Uninstalling it and switching back to default Jedi can solve it.

@EhsanKia
Copy link

EhsanKia commented Nov 3, 2020

This is the repo for pylance, so I assume the goal of this "issue" is to fix cv2 support on Pylance. Disabling/uninstalling pylance is a short term workaround but not really relevant here.

@YoniChechik
Copy link

Does the latest version (2020.11.2) really fixed this? (It said that it's solved in the first bullet here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#2020112-18-november-2020 )

@EhsanKia
Copy link

I tried 2020.11.2 and even 2020.11.3-pre.1 and couldn't get it to work.

I do see the included stub:
.vscode\extensions\ms-python.vscode-pylance-2020.11.3-pre.1\dist\bundled\native-stubs\cv2\cv2.pyi

But I'm not sure how to get pylance to use it. Manually copying the cv2.pyi file into PROJECT_ROOT/typings does work though. Anyone know why Pylance isn't using the bundled stub?

@huguesv
Copy link
Contributor

huguesv commented Nov 25, 2020

@YoniChechik and @EhsanKia

On 2020.11.2, when I do:

import cv2
cv2.

I get completions for everything in the compiled module (which has a lot), including imread. It is resolving to the file under native-stubs. Note that this is a stub that was created by scraping the native module, so it's not up to the same quality as the ones in typeshed, ie there are a lot of Any.

I'm testing on Windows. This is OS sensitive code, which OS are you on where it's not giving on completions on cv2?

@huguesv
Copy link
Contributor

huguesv commented Nov 25, 2020

Oh, also note that you will need to have opencv-python installed for this to work. Unlike the other bundled stubs, these native-stubs are partial and require the rest of the library to be present in order to resolve.

@EhsanKia
Copy link

I'm on windows 10, on Python 3.7.7 (venv) with the following installed:
opencv-contrib-python==4.2.0.34
opencv-python==4.1.2+contrib

and this is what I see: https://i.imgur.com/LS41Y0K.png
Completions don't work either.

As mentioned above, moving the stub to stubPath works, the module goes green and completion works.
But it does not work out of the bundled path. Is there any way to debug it? This is all I see in the language server console:

[Info  - 5:42:22 PM] Pylance language server 2020.11.2 (pyright 750a296b) starting
[Info  - 5:42:22 PM] Server root directory: c:\Users\Ehsan\.vscode\extensions\ms-python.vscode-pylance-2020.11.2\dist
[Error - 5:42:22 PM] stubPath c:\Users\Ehsan\Coding\Python\OpenCV\CatalogScanner\typings is not a valid directory.

@judej judej added the waiting for user response Requires more information from user label Dec 3, 2020
@judej
Copy link
Contributor

judej commented Dec 3, 2020

@EhsanKia have you tried getting completions on a clean project with just a new opencv-python? In my case I tried with opencv-python==4.4.0.44. As Hugues, mentioned above, I do get completions. I'd also recommend the latest version of Pylance. Please let me know if you still don't receive completions

@judej
Copy link
Contributor

judej commented Dec 29, 2020

Closing issue. Please reopen if necessary.

@judej judej closed this as completed Dec 29, 2020
@jahaniam
Copy link

jahaniam commented Feb 3, 2021

I'm using latest version of pylance (v2021.1.4-pre.1) and (opencv 4.5.1) and remote containers. The autocomplete doesn't work for me.

@jahaniam
Copy link

jahaniam commented Feb 3, 2021

I found the problem. My opencv was installed through sudo pip (in dockerfile RUN pip install python-opencv) which made things get messy when I have a user other than root:

/home/user/local/lib/python3.6/dist-packages/cv2
It should be:
/home/user/.local/lib/python3.6/site-packages/cv2

So I uninstalled and installed it without sudo by using

sudo pip uninstall opencv-python
pip install opencv-python --user

Things are working now.
I used vscode container script to create user and my guess is that pylance fails to check for /usr/bin/local when you login through a user into a container.(when /home/vscode/.local/ exist)

@jakebailey
Copy link
Member

Pylance uses the environment selected by the Python extension, and then calls that interpreter to get search paths (so we match the intrpreter). If you enable Pylance's trace logging, you should see those paths printed out. Did you see that first directory in the logs at all? I'm surprised pip as root would be installing there, as opposed to say /usr/ or something.

@jahaniam
Copy link

jahaniam commented Feb 3, 2021

@jakebailey Thank you for the quick reply.
It is very common to have RUN pip install opencv-python As root in a dockerfile.

Packages installed through dpkg/apt: /usr/lib/python3.6/dist-poackages
Packages through sudo pip: /usr/local/lib/python3.6/site-packages
Packages through pip —user: $HOME/.local/python3.6/site-packages

How can I enable the pylance tracing?

@jakebailey
Copy link
Member

See https://github.com/microsoft/pylance-release/blob/main/TROUBLESHOOTING.md#filing-an-issue, but I would recommend filing a new issue as this seems unrelated to the original issue. We would certainly support those directories, and the trace logs should show the paths in use.

@jahaniam
Copy link

jahaniam commented Feb 4, 2021

This only happens with opencv. I have tensorflow in the same folder /usr/local/lib/... pylance can recognise it.

The issue is easily reproducable.

The path is indeed in the interpreter. Something different for opencv package that should not be in ‘/bin/local’

@jahaniam
Copy link

jahaniam commented Feb 4, 2021

[Info  - 12:02:54 AM] Pylance language server 2021.1.3 (pyright 61c53da4) starting
[Info  - 12:02:54 AM] Server root directory: /home/vscode/.vscode-server-insiders/extensions/ms-python.vscode-pylance-2021.1.3/dist
[Info  - 12:02:54 AM] No configuration file found.
[Info  - 12:02:54 AM] Setting pythonPath for service "vscode": "/usr/bin/python3"
Search paths found for configured python interpreter:
  /usr/lib/python3.6
  /usr/lib/python3.6/lib-dynload
  /home/vscode/.local/lib/python3.6/site-packages
  /usr/local/lib/python3.6/dist-packages
  /usr/lib/python3/dist-packages
[Error - 12:02:54 AM] stubPath /home/vscode/typings is not a valid directory.
[Info  - 12:02:54 AM] Assuming Python version 3.6
[Info  - 12:02:54 AM] Assuming Python platform Linux
[Info  - 12:02:54 AM] Searching for source files
[Info  - 12:02:54 AM] Found 4249 source files
[Info  - 12:02:54 AM] Background analysis(1) root directory: /home/vscode/.vscode-server-insiders/extensions/ms-python.vscode-pylance-2021.1.3/dist
[Info  - 12:02:54 AM] Background analysis(1) started
Background analysis message: setConfigOptions
Background analysis message: setTrackedFiles
Background analysis message: markAllFilesDirty
Background analysis message: setFileOpened
Background analysis message: getSemanticTokens
[BG(1)] parsing: /home/vscode/x.py (15ms)
[BG(1)] parsing: /home/vscode/.vscode-server-insiders/extensions/ms-python.vscode-pylance-2021.1.3/dist/typeshed-fallback/stdlib/3/builtins.pyi [fs read 2ms] (128ms)
[BG(1)] binding: /home/vscode/.vscode-server-insiders/extensions/ms-python.vscode-pylance-2021.1.3/dist/typeshed-fallback/stdlib/3/builtins.pyi (48ms)
[BG(1)] binding: /home/vscode/x.py (0ms)
[BG(1)] parsing: /usr/local/lib/python3.6/dist-packages/cv2/__init__.py [fs read 0ms] (8ms)
[BG(1)] binding: /usr/local/lib/python3.6/dist-packages/cv2/__init__.py ...
[BG(1)]   parsing: /usr/local/lib/python3.6/dist-packages/cv2/data/__init__.py [fs read 0ms] (0ms)
[BG(1)]   binding: /usr/local/lib/python3.6/dist-packages/cv2/data/__init__.py (0ms)
[BG(1)] binding: /usr/local/lib/python3.6/dist-packages/cv2/__init__.py (3ms)
[BG(1)] parsing: /home/vscode/.vscode-server-insiders/extensions/ms-python.vscode-pylance-2021.1.3/dist/typeshed-fallback/stdlib/2and3/_typeshed/__init__.pyi [fs read 1ms] (13ms)
[BG(1)] binding: /home/vscode/.vscode-server-insiders/extensions/ms-python.vscode-pylance-2021.1.3/dist/typeshed-fallback/stdlib/2and3/_typeshed/__init__.pyi (2ms)
[BG(1)] parsing: /home/vscode/.vscode-server-insiders/extensions/ms-python.vscode-pylance-2021.1.3/dist/typeshed-fallback/third_party/2and3/typing_extensions.pyi [fs read 0ms] (6ms)
[BG(1)] binding: /home/vscode/.vscode-server-insiders/extensions/ms-python.vscode-pylance-2021.1.3/dist/typeshed-fallback/third_party/2and3/typing_extensions.pyi (2ms)
[BG(1)] parsing: /home/vscode/.vscode-server-insiders/extensions/ms-python.vscode-pylance-2021.1.3/dist/typeshed-fallback/stdlib/3/typing.pyi [fs read 2ms] (38ms)
[BG(1)] binding: /home/vscode/.vscode-server-insiders/extensions/ms-python.vscode-pylance-2021.1.3/dist/typeshed-fallback/stdlib/3/typing.pyi (12ms)
Background analysis message: getSemanticTokens
Background analysis message: analyze
[BG(1)] analyzing: /home/vscode/x.py ...
[BG(1)]   checking: /home/vscode/x.py (2ms)
[BG(1)] analyzing: /home/vscode/x.py (2ms)
Background analysis message: resumeAnalysis

@jahaniam
Copy link

jahaniam commented Feb 4, 2021

I think the issue is quite relevant. I cannot get the cv2 IntelliSense if the package is installed in /usr/local

test

The solution is to uninstall and install it under /$HOME/.local

@metal3d
Copy link

metal3d commented Jul 1, 2021

To make the stub file quiclkly:

stubgen -m cv2 -o $(python -c 'import cv2, os; print(os.path.dirname(cv2.__file__))')

@bschnurr
Copy link
Member

bschnurr commented Jul 5, 2021

here are some work in progress stubs
microsoft/python-type-stubs#72

@parsa-ra
Copy link

Make sure to change the name of generated stub file from mypy (e.g. cv2.pyi) to __init__.pyi.

@2easy4wizzi
Copy link

parsa-ra - thank god for your comment

@Apisteftos
Copy link

Maybe we can manually use stubgen to generate .pyi as a workaround.

  1. pip install mypy
  2. stubgen -m cv2 -o {CV2_PACKAGE_FOLDER}

Works fine for PyCharm 2022.3.1 and Intellij. Unfortunately there is no other solution than this.
For PyCharm you need couple more steps.

  1. Open the generated file in venv/Lib/site-packages/cv2/__init__.pyi
  2. In the file there is def bootstrap() function.
  3. From the directory C:\Users\uresname\.vscode\extensions\ms-python.vscode-pylance-2023.1.20\dist\bundled\stubs\cv2-stubs\__init__.pyi you will find the __init__.pyi file. Copy the code and paste it in the venv/Lib/site-packages/cv2/__init__.pyi and delete the def bootstrap() function.

To avoid all these steps. Just copy the file C:\Users\uresname\.vscode\extensions\ms-python.vscode-pylance-2023.1.20\dist\bundled\stubs\cv2-stubs\__init__.pyi into the venv/Lib/site-packages/cv2 and it will work fine.

@rosejn
Copy link

rosejn commented Jan 12, 2024

Even without a stub file that produces detailed signatures with types it seems like pylance should be capable of looking up method and attribute names, which would likely solve most of what people are looking for. Intellisense is especially nice for a large package like this just to help you find the right names. Clearly the stub generation is problematic, or this could have happened years ago.

@bschnurr
Copy link
Member

bschnurr commented Jan 12, 2024

the latest opencv package should be shipping with its own stubs. Is that not working? please create a new issue then.

see #4838 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs stub waiting for user response Requires more information from user
Projects
None yet
Development

No branches or pull requests