We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload 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.
@vscode/python-extension npm module carries types and helper utilities to access the feature. Follow instructions there to add Python extension and module as a dependency to your extension first. Then,
@vscode/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 }; };
Toggle table of contents Pages 41 Loading Home Uh oh! There was an error while loading. Please reload this page. Loading AB Experiments Uh oh! There was an error while loading. Please reload this page. Loading Activate Environments in Terminal Using Environment Variables Uh oh! There was an error while loading. Please reload this page. Loading Areas of Ownership for vscode‐python Team Uh oh! There was an error while loading. Please reload this page. Loading Coding Uh oh! There was an error while loading. Please reload this page. Loading Coding Guidelines Uh oh! There was an error while loading. Please reload this page. Loading Commands Uh oh! There was an error while loading. Please reload this page. Loading Components Uh oh! There was an error while loading. Please reload this page. Loading Components Test Management Uh oh! There was an error while loading. Please reload this page. Loading Dependency Injection with Inversify Uh oh! There was an error while loading. Please reload this page. Loading Design Docs Uh oh! There was an error while loading. Please reload this page. Loading Experiments Uh oh! There was an error while loading. Please reload this page. Loading Extension Activation Uh oh! There was an error while loading. Please reload this page. Loading Fixing "deactivate" command for virtual environments Uh oh! There was an error while loading. Please reload this page. Loading Interpreter and Environment Discovery Uh oh! There was an error while loading. Please reload this page. Loading Issue Management Uh oh! There was an error while loading. Please reload this page. Loading Limited support for Python 2.7 Uh oh! There was an error while loading. Please reload this page. Loading Migration to Python Tools Extensions Uh oh! There was an error while loading. Please reload this page. Loading Mono Repo Set‐up Guide Uh oh! There was an error while loading. Please reload this page. Loading New Method for Output Handling in Python Testing Uh oh! There was an error while loading. Please reload this page. Loading Partial mode Uh oh! There was an error while loading. Please reload this page. Loading Preview of the new Python bundle in Visual Studio Code Uh oh! There was an error while loading. Please reload this page. Loading Product Package Management Uh oh! There was an error while loading. Please reload this page. Loading Profiling Uh oh! There was an error while loading. Please reload this page. Loading Python Environment APIs Get the Python extension API Extension API Usage Get the active environment to run some script Change the current active environment Get notified when active environment is changed Get current known environments Detect new environments or changes to the interpreters discovered in the system Get all details for a particular environment Get all details for all environments in the system Look for a particular environment and set it Upcoming potential APIs Allow to register a provider Allow execution in environments Request for getActivatedEnvironmentVariables and onDidChangeGetEnvironmentVariables Loading Python Environment Search Uh oh! There was an error while loading. Please reload this page. Loading Remove the need for reloading VS Code when changing language servers Uh oh! There was an error while loading. Please reload this page. Loading Reporting a bug Uh oh! There was an error while loading. Please reload this page. Loading Roadmap Uh oh! There was an error while loading. Please reload this page. Loading Setting descriptions Uh oh! There was an error while loading. Please reload this page. Loading SLAs Uh oh! There was an error while loading. Please reload this page. Loading Sort environments by assumed usefulness Uh oh! There was an error while loading. Please reload this page. Loading Stand Up Uh oh! There was an error while loading. Please reload this page. Loading Submitting a pull request Uh oh! There was an error while loading. Please reload this page. Loading Support poetry virtual environments Uh oh! There was an error while loading. Please reload this page. Loading Testing Uh oh! There was an error while loading. Please reload this page. Loading Testing Architecture Review Uh oh! There was an error while loading. Please reload this page. Loading Troubleshoot ' 4058 ENOENT' error from NodeJS ‐ Python interpreter cannot be found Uh oh! There was an error while loading. Please reload this page. Loading Troubleshooting TensorBoard integration in VS Code Uh oh! There was an error while loading. Please reload this page. Loading Updating tooling versions Uh oh! There was an error while loading. Please reload this page. Loading VS Code Extension API Uh oh! There was an error while loading. Please reload this page. Show 26 more pages… Usage Roadmap Documentation Reporting a bug A/B experiments TensorBoard integration Development Issue Management Coding Testing Submitting a pull request Design Docs SLAs Stand-Up Updating tooling support Reference Environment API Python Environment Search Clone this wiki locally