microsoft / pyright

Static Type Checker for Python
Other
13.43k stars 1.47k forks source link

Can't resolve opencv module #209

Closed spindensity closed 5 years ago

spindensity commented 5 years ago

Pyright can't resolve opencv module but actually the statement import cv2 has been successful.

Snipaste_2019-08-19_23-12-20

OS: Win10 1903 64bit Python: Miniconda3 with python 3.7.4 environment OpenCV: 3.4.2 installed by conda install opencv Pyright: 1.0.56 VS Code: 1.37.1

erictraut commented 5 years ago

You need to configure pyright so it knows how to find your imports.

For more details, see this issue: https://github.com/microsoft/pyright/issues/194 And refer to this documentation: https://github.com/microsoft/pyright/blob/master/docs/import-resolution.md

spindensity commented 5 years ago

I believe I configured pyright correctly. The problem is not in connection with configurations. I think the root cause is that pyright can't resolve dynamic link library that contains a python module.(pyd module).

Here's the detail:

I created a conda environment named "datascience", installed tensorflow, opencv, numpy and some other packages in that environment: evd1

I set python.pythonPath correctly and VS Code actually used that environment, see the red rectangle: evd3 And you can see all packages such as tensorflow, numpy are resolved by pyright with no problem but opencv.

We also could see opencv module in the conda environment site-pacakges directory: evd2 The module file name is cv2.cp37-win_amd64.pyd

Here is the log produced by pyright, pyright tried to find .py file, pyi file in the right environment but nerver tried to find pyd file, then reported errors.

Pyright language server starting
Starting service instance "UGATIT"
Starting service instance "<default>"
Fetching settings for workspace(s)
Received updated settings
No configuration file found.
Setting pythonPath for service "UGATIT": "C:\Dev\BasicTools\python\miniconda3\envs\datascience\python.exe"
Searching for source files
No source files found.
Could not import 'cv2' in file 'c:\Dev\Projects\deep-learning\UGATIT\utils.py'
  Looking for typeshed stdlib path
  Attempting to resolve using root path 'C:\Dev\BasicTools\python\typeshed\stdlib\3.7'
  Could not find directory 'C:\Dev\BasicTools\python\typeshed\stdlib\3.7\cv2'
  Did not find file 'C:\Dev\BasicTools\python\typeshed\stdlib\3.7\cv2.pyi' or 'C:\Dev\BasicTools\python\typeshed\stdlib\3.7\cv2.py'
  Attempting to resolve using root path 'C:\Dev\BasicTools\python\typeshed\stdlib\3.6'
  Could not find directory 'C:\Dev\BasicTools\python\typeshed\stdlib\3.6\cv2'
  Did not find file 'C:\Dev\BasicTools\python\typeshed\stdlib\3.6\cv2.pyi' or 'C:\Dev\BasicTools\python\typeshed\stdlib\3.6\cv2.py'
  Attempting to resolve using root path 'C:\Dev\BasicTools\python\typeshed\stdlib\3.5'
  Could not find directory 'C:\Dev\BasicTools\python\typeshed\stdlib\3.5\cv2'
  Did not find file 'C:\Dev\BasicTools\python\typeshed\stdlib\3.5\cv2.pyi' or 'C:\Dev\BasicTools\python\typeshed\stdlib\3.5\cv2.py'
  Attempting to resolve using root path 'C:\Dev\BasicTools\python\typeshed\stdlib\3'
  Could not find directory 'C:\Dev\BasicTools\python\typeshed\stdlib\3\cv2'
  Did not find file 'C:\Dev\BasicTools\python\typeshed\stdlib\3\cv2.pyi' or 'C:\Dev\BasicTools\python\typeshed\stdlib\3\cv2.py'
  Attempting to resolve using root path 'C:\Dev\BasicTools\python\typeshed\stdlib\2and3'
  Could not find directory 'C:\Dev\BasicTools\python\typeshed\stdlib\2and3\cv2'
  Did not find file 'C:\Dev\BasicTools\python\typeshed\stdlib\2and3\cv2.pyi' or 'C:\Dev\BasicTools\python\typeshed\stdlib\2and3\cv2.py'
  Typeshed path not found
  Looking in root directory of execution environment 'c:\Dev\Projects\deep-learning\UGATIT'
  Attempting to resolve using root path 'c:\Dev\Projects\deep-learning\UGATIT'
  Could not find directory 'c:\Dev\Projects\deep-learning\UGATIT\cv2'
  Did not find file 'c:\Dev\Projects\deep-learning\UGATIT\cv2.pyi' or 'c:\Dev\Projects\deep-learning\UGATIT\cv2.py'
  Looking for typeshed path
  Looking for typeshed third_party path
  Attempting to resolve using root path 'C:\Dev\BasicTools\python\typeshed\third_party\3.5'
  Could not find directory 'C:\Dev\BasicTools\python\typeshed\third_party\3.5\cv2'
  Did not find file 'C:\Dev\BasicTools\python\typeshed\third_party\3.5\cv2.pyi' or 'C:\Dev\BasicTools\python\typeshed\third_party\3.5\cv2.py'
  Attempting to resolve using root path 'C:\Dev\BasicTools\python\typeshed\third_party\3'
  Could not find directory 'C:\Dev\BasicTools\python\typeshed\third_party\3\cv2'
  Did not find file 'C:\Dev\BasicTools\python\typeshed\third_party\3\cv2.pyi' or 'C:\Dev\BasicTools\python\typeshed\third_party\3\cv2.py'
  Attempting to resolve using root path 'C:\Dev\BasicTools\python\typeshed\third_party\2and3'
  Could not find directory 'C:\Dev\BasicTools\python\typeshed\third_party\2and3\cv2'
  Did not find file 'C:\Dev\BasicTools\python\typeshed\third_party\2and3\cv2.pyi' or 'C:\Dev\BasicTools\python\typeshed\third_party\2and3\cv2.py'
  Typeshed path not found
  Looking in python search path 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\DLLs'
  Attempting to resolve using root path 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\DLLs'
  Could not find directory 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\DLLs\cv2'
  Did not find file 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\DLLs\cv2.pyi' or 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\DLLs\cv2.py'
  Looking in python search path 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib'
  Attempting to resolve using root path 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib'
  Could not find directory 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\cv2'
  Did not find file 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\cv2.pyi' or 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\cv2.py'
  Looking in python search path 'C:\Dev\BasicTools\python\miniconda3\envs\datascience'
  Attempting to resolve using root path 'C:\Dev\BasicTools\python\miniconda3\envs\datascience'
  Could not find directory 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\cv2'
  Did not find file 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\cv2.pyi' or 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\cv2.py'
  Looking in python search path 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages'
  Attempting to resolve using root path 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages'
  Could not find directory 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\cv2'
  Did not find file 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\cv2.pyi' or 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\cv2.py'
  Looking in python search path 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\win32'
  Attempting to resolve using root path 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\win32'
  Could not find directory 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\win32\cv2'
  Did not find file 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\win32\cv2.pyi' or 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\win32\cv2.py'
  Looking in python search path 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\win32\lib'
  Attempting to resolve using root path 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\win32\lib'
  Could not find directory 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\win32\lib\cv2'
  Did not find file 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\win32\lib\cv2.pyi' or 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\win32\lib\cv2.py'
  Looking in python search path 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\Pythonwin'
  Attempting to resolve using root path 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\Pythonwin'
  Could not find directory 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\Pythonwin\cv2'
  Did not find file 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\Pythonwin\cv2.pyi' or 'C:\Dev\BasicTools\python\miniconda3\envs\datascience\lib\site-packages\Pythonwin\cv2.py'
Setting pythonPath for service "<default>": "C:\Dev\BasicTools\python\miniconda3\envs\datascience\python.exe"
Searching for source files
No source files found.
erictraut commented 5 years ago

Ah yes, you're correct - pyright doesn't know anything about pyd files.

I see you've opened a separate issue - thanks. I'll re-close this one and use the other to track the issue.

Redhawk18 commented 1 month ago

I'm still having these problems, everything is through nix as my package manager.

txk2048 commented 1 day ago

I'm still having these problems, everything is through nix as my package manager.

AFAIK it's not possible for pyright to provide completions for OpenCV on Linux because the bindings are shipped directly as a .so file and do not ship any types directly.

Redhawk18 commented 1 day ago

Maybe fix the issue, instead of just deciding for the python community that this library doesn't require LSP support? To tell everyone, "well they do their project slightly differently, so we can't cope with it." Is disrespectful to your users.

bschnurr commented 1 day ago

checkout a newer version of opencv with type stubs
https://github.com/opencv/opencv/pull/20370 https://github.com/opencv/opencv/releases/tag/4.8.0

txk2048 commented 1 day ago

For NixOS/nixpkgs users, please note that 24.05 does not ship the Python type stubs, this has been fixed in the latest nixos-unstable and the fix will land in 24.11.

Issue fixed by the following commit: https://github.com/NixOS/nixpkgs/commit/197f37c80fbef3319f102434a5b98de0d54aa2b9