From 96fe803094db0a7ef8564c1ac4c87214089c8e72 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Thu, 12 Jan 2023 13:08:58 +1100 Subject: [PATCH] Command to install Python into a conda env --- src/client/interpreter/interpreterService.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/client/interpreter/interpreterService.ts b/src/client/interpreter/interpreterService.ts index 50545558d721..1ec66edf9271 100644 --- a/src/client/interpreter/interpreterService.ts +++ b/src/client/interpreter/interpreterService.ts @@ -2,6 +2,7 @@ import { inject, injectable } from 'inversify'; import * as pathUtils from 'path'; import { + commands, ConfigurationChangeEvent, Disposable, Event, @@ -38,6 +39,7 @@ import { EventName } from '../telemetry/constants'; import { cache } from '../common/utils/decorators'; import { PythonLocatorQuery, TriggerRefreshOptions } from '../pythonEnvironments/base/locator'; import { sleep } from '../common/utils/async'; +import { PythonEnvKind } from '../pythonEnvironments/base/info'; type StoredPythonEnvironment = PythonEnvironment & { store?: boolean }; @@ -90,6 +92,10 @@ export class InterpreterService implements Disposable, IInterpreterService { this.configService = this.serviceContainer.get(IConfigurationService); this.interpreterPathService = this.serviceContainer.get(IInterpreterPathService); this.onDidChangeInterpreters = pyenvs.onChanged; + commands.registerCommand('python.installPythonIntoCondaEnv', async (pythonPath: string) => { + await this.ensureEnvironmentContainsPythonImpl(pythonPath); + this.triggerRefresh({ kinds: [PythonEnvKind.Conda] }); + }); } public async refresh(resource?: Uri): Promise { @@ -236,15 +242,24 @@ export class InterpreterService implements Disposable, IInterpreterService { if (!(await installer.isInstalled(Product.python))) { // If Python is not installed into the environment, install it. sendTelemetryEvent(EventName.ENVIRONMENT_WITHOUT_PYTHON_SELECTED); + await this.ensureEnvironmentContainsPythonImpl(pythonPath); + } + } + + private async ensureEnvironmentContainsPythonImpl(pythonPath: string) { + const installer = this.serviceContainer.get(IInstaller); + const interpreterInfo = await this.getInterpreterDetails(pythonPath); + if (!(await installer.isInstalled(Product.python, interpreterInfo))) { + // If Python is not installed into the environment, install it. const shell = this.serviceContainer.get(IApplicationShell); const progressOptions: ProgressOptions = { location: ProgressLocation.Window, title: `[${Interpreters.installingPython}](command:${Commands.ViewOutput})`, }; traceLog('Conda envs without Python are known to not work well; fixing conda environment...'); - const promise = installer.install(Product.python, await this.getInterpreterDetails(pythonPath)); + const promise = installer.install(Product.python, interpreterInfo); shell.withProgress(progressOptions, () => promise); - promise.then(() => this.triggerRefresh().ignoreErrors()); + await promise.then(() => this.triggerRefresh().ignoreErrors()); } } }