matangover / mypyls

Mypy language server: runs mypy on Python code to provide type checking, go to definition, and hover.
MIT License
34 stars 5 forks source link

Use default python_executable when pythonPath is not a filepath #4

Closed travbid closed 4 years ago

travbid commented 4 years ago

I have a python file:

import grpc

After installing the dependency via pip3 install grpcio, the mypy-vscode extension reports this error example.py:1:1: error: Cannot find implementation or library stub for module named 'grpc'

This happens because:

This commit only overrides the default options.python_executable if ours is not None. I don't know if this is the best way to solve the problem, but I think leaving options.python_executable as its default is a reasonable thing to do.

matangover commented 4 years ago

Thanks for the PR! Good catch. I will take a closer look soon. I'm hesitating to accept because IIUC this means that in the default case, this may create confusion: the user will install a module to system Python, but mypyls won't find it because it would use the Python on which it is installed (a virtualenv, if you follow my recommended installation instructions). The only 'correct' solution I guess is to expand 'python' using the system path, mimicking the behavior of vscode-python extension. In fact, vscode-python is now in the process of deprecating the pythonPath setting...

matangover commented 4 years ago

@travbid Thanks very much for debugging this issue and for the PR. I agree your solution is good for now. Going forward, the best would be to use the new API from the Python extension to get the executable path:

https://github.com/microsoft/pyright/issues/697 https://github.com/formulahendry/vscode-code-runner/issues/604 https://github.com/microsoft/vscode-python/issues/11294

Anyway users can always just configure their path in mypy.ini, this whole interpreter selection in VSCode is quite a mess...

karrtikr commented 4 years ago

@matangover This code snippet shows how to use the new API, could be helpful.