GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Python Environment APIs Python Environment APIs
Kartik Raj edited this page
These APIs provide a way for extensions to work with Python environments available in the user's machine as found by the Python extension.
// Import the API import { PythonExtension } from '@vscode/python-extension'; ... // Load the Python extension API const pythonApi: PythonExtension = await PythonExtension.api(); const environments = pythonApi.environments; // This will return something like /usr/bin/python const environmentPath = environments.getActiveEnvironmentPath(); // `environmentPath.path` carries the value of the setting. Note that this path may point to a folder and not the // python binary. Depends entirely on how the env was created. // E.g., `conda create -n myenv python` ensures the env has a python binary // `conda create -n myenv` does not include a python binary. // Also, the path specified may not be valid, use the following to get complete details for this environment if // need be. const environment = await environments.resolveEnvironment(environmentPath); if (environment) { // run your script here. } await environments.updateActiveEnvironment('/bin/usr/python'); let currentActivePython = undefined; extContext.subscriptions.push( environments.onDidChangeActiveEnvironment((e: ActiveEnvironmentPathChangeEvent) => { currentActivePython = e.path; }), ); // Get currently selected environment. currentActivePython = environments.getActiveEnvironmentPath()?.path; // Use this way if you don't really want to wait for the extension to fully load info for all environments. const environments = environments.known; const environments: string[] | undefined; extContext.subscriptions.push( environments.onDidEnvironmentsChanged((e: EnvironmentsChangeEvent) => { if (environments) { // handle changes here based on add, remove, update } }), ); // Get the current list of environments. environments = environments.known; const detail = environments.resolveEnvironment(`usr/bin/python`); // Trigger a full refresh and wait for it to complete. await environments.refreshEnvironments(); const environments = environments.known; const envs = environments.known; const foundEnv = envs.find(p => p.name === 'envName'); if (foundEnv) { await environments.updateActiveEnvironment(foundEnv); } else { // Could not find the path, maybe a refresh is needed? await environments.refreshEnvironments(); const foundEnv = envs.find(p => p.name === 'envName'); if (foundEnv) { await environments.updateActiveEnvironment(foundEnv); } else { // Environment not found in the system. } } registerEnvironmentProvider( environmentProvider: IEnvironmentProvider, metadata: EnvironmentProviderMetadata, ): Disposable; run: { // Functions would only require the arguments. The env provider can internally decide on the commands. exec: Function; shellExec: Function; // Only for backwards compatibility. execObservable: Function; /** * Uses a VSCode terminal. * */ terminalExec: () => void; /** * Any environment variables that can be used to activate the environment, if supported. * If not provided, Python extension itself uses the other execution APIs to calculate it. */ env?: { [key: string]: string | null | undefined }; }; https://github.com/microsoft/vscode-python/issues/15112 This can be problematic, although the request asks for We could revisit this when we have better activation story for ourselves. Clone this wiki locally |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |