Home > OS >  Get notification in a VS Code extension when the Python interpreter is changed
Get notification in a VS Code extension when the Python interpreter is changed

Time:01-31

I am writing a VS Code extension that depends on the currently set Python interpreter. When I change the Python Interpreter via the VS Code UI, the extension needs to refresh and get the latest Python path (mainly to show the right environment settings in the TreeView). For now, I have a refresh button in my custom TreeView that I need to press after selecting a different Python interpreter.

However, this is a second manual step. Is there a way to get a notification in my extension, when a user changes the Python Interpreter, e.g., an event the extension can listen to?

I only found VS Code's Activation Events, but it doesn't look like this would help. I didn't find any other events that get triggered after the command python.setInterpreter is executed

CodePudding user response:

Finally found it. The right config to watch for is python.defaultInterpreterPath

vscode.workspace.onDidChangeConfiguration(event => {
    let affected = event.affectsConfiguration("python.defaultInterpreterPath");
    if (affected) {
        doSomething();
    }
});

To support the usingNewInterpreterStorage case (default today), add:

const extension = vscode.extensions.getExtension('ms-python.python')!;
await extension.activate();
extension.exports.settings.onDidChangeExecutionDetails((event: any) => {
    doSomething();
});
  • Related