-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Handle python.pythonPath
deprecation in the Microsoft Python extension
#697
Comments
This should be similar to the fix applied to the Code Runner extension: https://github.com/formulahendry/vscode-code-runner/blob/2bed9aeeabc1118a5f3d75e47bdbcfaf412765ed/src/utility.ts#L6 |
Well, that, and the LSP middleware change to ensure the server always sees the setting anyway: https://github.com/microsoft/vscode-python/pull/11084/files#diff-ed95fdd33556d8f532e3324d9fb399c9R87 |
This will be implemented in the next release of Pyright. |
This is now implemented in version 1.1.45, which I just published. |
I'm trying it but I get this message after I remove [Info - 9:41:19 a.m.] Pyright language server 1.1.45 starting Am I missing a setting somewhere?
|
Hmm, that error message isn't being printed by Pyright. It seems to indicate that you don't have the Python extension installed (or enabled in your workspace). The new pythonPath mechanism requires it to be installed. Can you double check that you have it installed? When I was implementing and testing this feature, it wasn't clear to me how to completely disable the "python.pythonPath" setting. While this setting is being deprecated, the Python extension's environment selection feature appears to write the path to the selected Python environment to both the old "python.pythonPath" setting as well as the new extension-specific config store. I presume that at some point it will no longer update the old setting. |
I do have both the Microsoft Python extension and Pyright installed. I suspect it has to do with extension startup order. I think Pyright starts up before the Microsoft Python extension has started up and actually "loaded" my virtual environment. I did the following to test this theory:
PyRight seems to work properly now and there are no error messages. |
OK, thanks for those clues. I do have code in place that waits for the Python extension to start, but perhaps it's not working as intended. I'll look into it. |
I think this is what happens when you just run bare "python" on Windows; "python" is this magic binary which points the user to install something on the Windows Store, then is replaced once that's done. I've noticed this in testing in CI where I can see it printing out "Python 2.7.15" or similar in tests because "python" is being executed; I'd be curious if it's still being over-run when things are unset (where the most recent fixes were intended to only run Python if pythonPath was set), or some bug where the wrong executable is executed. |
I think Jake is correct that the message you're seeing is because no pythonPath was found, so Pyright is attempting to execute "python" as a fallback mechanism. With that mystery solved, we now need to understand why pythonPath isn't being read from the Python extension. I have code in place that should wait until the Python extension is activated, and it appears to work on my machine. Perhaps the problem is timing-related or is specific to Windows. I've added more logging to the Output window to help us diagnose it further. |
Do you wait until the Python extension is activated or until the Python extension has activated and activated the virtual environment? To ask another way, does Pyright get notified if someone clicks on the status bar and changes the Python virtual environment in use? |
I wait until the extension is activated. The Python extension doesn't currently expose any mechanism for notifying when someone activates or changes a virtual environment. We're looking at whether the Python extension could add an event of some sort. |
I think that might be the issue here. And it might affect more than just Pyright. As a workaround, would be it possible to add a command to Pyright e.g. "Reload Pyright pythonPath" or something like that that I could access from the command palette? |
That could explain what's happening. I'm not able to repro it on my machine, but it could be timing-related. I've added a bunch of additional logging that will be in the next version of Pyright to help us diagnose it further. Pyright already has a command for restarting the language server ("Pyright: Restart Server"). This is effectively what happens when a new pythonPath is received. |
I just published Pyright 1.1.46, which contains additional logging to the output pane. @sambhare, could you please try to repro the problem with this new version and let me know what output you see? Thanks! |
On a fresh start of VS Code and opening a folder with a venv set, I see this
(I don't have python.exe on my Windows PATH by design as I always work in venvs) If I do Pyright: Restart Server, nothing more is printed If I disable Pyright, reload VS Code, wait for Microsoft Python to show my venv on the status bar and then enable Pyright again I see this and it works:
|
Could be the auto interpreter picking; I'd be curious to see the Python extension's logs to see if it's picking the default and then changing it after. |
Python extension logs on fresh start
Snipped the actual flake output.Apparently I'm in a few telemetry experiments. Python language server output
I think the Python extension seems to be searching for Python on my machine until it finally gets to the one I've set in my virtualenv. |
Odd, doesn't say anything about just "python". Could be that it's the "default" setting being picked up (the extension has that notion internally, and it might be "python"). Unfortunately the logs don't state the reason an interpreter was chosen. You should probably set your |
OK, looks like it's safe to say that the Python extension is initially returning "python" until it finds the selected venv. When it does, it has no way of notifying other extensions. Until a notification mechanism is added, I don't see a good way to fix this. I suppose we could add a periodic polling mechanism, but that has a bunch of downsides. We'll just leave this bug open for now and wait for a notification mechanism to be implemented in the Python extension. |
@karthiknadig & @jakebailey, I implemented support for the new onDidChangeExecutionDetails notification callback in the Python extension. Works great! If you see anything wrong with my PR or have suggestions for a better approach, let me know. c89b6a5 |
The commit appears to do: |
In the core extension, I did this in a type safe manner without the string: https://github.com/microsoft/vscode-python/pull/12366/files#diff-53a0b543317dd8025be737a600e0fbd5R113 |
Good catch. The comment was right, the code was wrong. I like your solution better, so I'm going to copy that. I'm surprised my previous code worked as well as it appeared to. |
This is now implemented in Pyright 1.1.55, which I just published. |
With version 1.1.55, the proper conda env set to the interpreter, and no value specified for pythonPath in settings.json, I am getting the following Pyright errors for third party modules: |
@timothybrooks, what version of the Python extension are you running? Is it the latest (v2020.7.94776) or newer? |
Yes, I am using ms-python.python v2020.7.94776 and ms-pyright v1.1.55 |
@timothybrooks, you asked "Is Pyright using a different python than the interpreter?" It is meant to be using the same interpreter that is selected in the Python extension using its interpreter selector. Is that what you mean? As you're aware, the Python extension previously stored the path to the selected interpreter in the If you open the Output panel and choose "Pyright", you should see a log output that looks something like this: I'll mention that one workaround is to switch from the Pyright extension to Pylance. Pylance supports all of the functionality of Pyright but is more tightly integrated with the Python extension, so it doesn't need to use this interface to query the selected python interpreter. |
Thanks for clarifying what's going on. Here is the Pyright output:
I see no additional output when selecting a new interpreter. (And regarding the stubPath error, I do not specify any stubPath in my settings). Thanks for the help! |
The behavior you're reporting matches the behavior of older versions of the Python extension, prior to v2020.7.94776. That was the first version that introduced the new notification mechanism. A newer version of the Python extension (v2020.7.96456) was released today. Please give that a try. If you're still seeing this behavior, please look in the "Python" output to see if there are any clues. It should log something like "Python interpreter path: xxx" when you select a new interpreter from the selector. |
Is your feature request related to a problem? Please describe.
The
python.pythonPath
setting is being deprecated in the Microsoft Python extension. Currently Pyright seems (?) to use this setting to select the correct virtual environment. Removingpython.pythonPath
from my.vscode/settings.json
file breaks Pyright's selection of virtual environment.Describe the solution you'd like
Update the Pyright VS Code extension to handle this change in the Microsoft Python extension.
The text was updated successfully, but these errors were encountered: