microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.34k stars 1.19k forks source link

Jupyter API to get Env associated with Notebooks #24358

Open DonJayamanne opened 1 month ago

DonJayamanne commented 1 month ago

See https://github.com/microsoft/vscode-jupyter/issues/15987

Should also fix https://github.com/microsoft/vscode-jupyter/issues/16112 Should also avoid Pylance having to monitor notebook changes and then trying to figure out the Environment for a Notebook.

@rchiodo @heejaechang @debonte Please can you let me know if this works I.e. going forward (we can plan how we roll this out)

I believe this should simplify things. @karthiknadig /cc

rchiodo commented 1 month ago

Pylance having to monitor notebook changes

For what it's worth, we don't do this now. The vscode-languageclient-npm module does. So theoretically any language can listen for notebook events, not just Pylance.

DonJayamanne commented 1 month ago

So theoretically any language can listen for notebook events, not just Pylance.

How does Pylance get the Python env associated with a notebook?

rchiodo commented 1 month ago

How does Pylance get the Python env associated with a notebook?

We ask the python extension for the environment for the current file. This happens after we get the notebook changed event.

DonJayamanne commented 1 month ago

We ask the python extension for the environment for the current file.

awesome, then this change will make this workflow work properly

What I'm getting at is Pylance will not need the registerJupyterPythonPathFunction (private) api anymore and you can use the official Python extension API. this way others language servers can benefit from this, else if some other extension like Ruff or Jedi (if there were other extension) need this, then they would need a new API or a similar hook...

I.e. just use getActiveEnvironmentPath(resource?: Resource): EnvironmentPath and onDidChangeActiveEnvironmentPath to detect changes to Python envs associated with Notebooks and the like (still free to use notebook change events as well).