Closed travbid closed 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...
@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...
@matangover This code snippet shows how to use the new API, could be helpful.
I have a python file:
After installing the dependency via
pip3 install grpcio
, the mypy-vscode extension reports this errorexample.py:1:1: error: Cannot find implementation or library stub for module named 'grpc'
This happens because:
By default, VSCode sets "python.pythonPath" to "python"
mypy_server.py
fetches this value inconfiuration_changed()
got_python_executable()
decides that this is not a file that can be executed and setspython_executable
toNone
.start_server_and_analyze()
then setsoptions.python_executable
from its default ofsys.executable
to that value.Because options.python_executable is
None
,modulefinder.py
in mypy simply returns[]
for site-packages: https://github.com/python/mypy/blob/63f2fed5d0e10d4d875442e9bba2283c81dfdf4b/mypy/modulefinder.py#L465Thus, mypy cannot find my dependency and returns:
example.py:1:1: error: Cannot find implementation or library stub for module named 'grpc'
This commit only overrides the default
options.python_executable
if ours is notNone
. I don't know if this is the best way to solve the problem, but I think leavingoptions.python_executable
as its default is a reasonable thing to do.