forked from DonJayamanne/vscode-python-manager
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvenvSwitchPython.ts
More file actions
30 lines (28 loc) · 1.26 KB
/
Copy pathvenvSwitchPython.ts
File metadata and controls
30 lines (28 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as path from 'path';
import { Disposable, Uri, extensions } from 'vscode';
import { PVSC_EXTENSION_ID, PythonExtension } from '@vscode/python-extension';
import { createDeferred } from '../../../common/utils/async';
import { traceInfo } from '../../../logging';
export async function switchSelectedPython(interpreter: string, uri: Uri, purpose: string): Promise<void> {
let dispose: Disposable | undefined;
try {
const deferred = createDeferred<void>();
const api = extensions.getExtension<PythonExtension>(PVSC_EXTENSION_ID)?.exports;
if (!api) {
throw new Error('Api not exported by Python extension')
}
dispose = api.environments.onDidChangeActiveEnvironmentPath(async (e) => {
if (path.normalize(e.path) === path.normalize(interpreter)) {
traceInfo(`Switched to interpreter ${purpose}: ${interpreter}`);
deferred.resolve();
}
});
api.environments.updateActiveEnvironmentPath(interpreter, uri);
traceInfo(`Switching interpreter ${purpose}: ${interpreter}`);
await deferred.promise;
} finally {
dispose?.dispose();
}
}