GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — vscode-python/pythonExtensionApi at main · dsanders11/vscode-python

Expand file tree

/

pythonExtensionApi

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
 
 
/

pythonExtensionApi

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
src
src
 
 
.eslintrc
.eslintrc
 
 
.npmignore
.npmignore
 
 
LICENSE.md
LICENSE.md
 
 
README.md
README.md
 
 
SECURITY.md
SECURITY.md
 
 
package-lock.json
package-lock.json
 
 
package.json
package.json
 
 
tsconfig.json
tsconfig.json
 
 

README.md

Outline

Python extension's API

This npm module implements an API facade for the Python extension in VS Code.

Example

First we need to define a package.json for the extension that wants to use the API:

{
	"name": "...",
	...
	// depend on the Python extension
	"extensionDependencies": [
		"ms-python.python"
	],
	// Depend on the Python extension facade npm module to get easier API access to the
	// core extension.
	"dependencies": {
		"@vscode/python-extension": "...",
		"@types/vscode": "..."
	},
}

Update "@types/vscode" to a recent version of VS Code, say "^1.81.0" for VS Code version "1.81", in case there are any conflicts.

The actual source code to get the active environment to run some script could look like this:

// Import the API
import { PythonExtension } from '@vscode/python-extension';

...

// Load the Python extension API
const pythonApi: PythonExtension = await PythonExtension.api();

// This will return something like /usr/bin/python
const environmentPath = pythonApi.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 pythonApi.environments.resolveEnvironment(environmentPath);
if (environment) {
    // run your script here.
}

Check out the wiki for many more examples and usage.

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.