Skip to content

Commit f4d7556

Browse files
author
Kartik Raj
authored
Update README for Python extension API to point to examples (#21638)
For #20949
1 parent f38f6e5 commit f4d7556

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

build/azure-pipelines/pipeline.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
###############################################################################################
55
name: $(Date:yyyyMMdd)$(Rev:.r)
66

7+
trigger: none
8+
79
pr: none
810

911
resources:

pythonExtensionApi/README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ This npm module implements an API facade for the Python extension in VS Code.
44

55
## Example
66

7-
The source code of the example can be found [here](TODO Update example extension link here)
8-
97
First we need to define a `package.json` for the extension that wants to use the API:
108

119
```jsonc
@@ -24,8 +22,29 @@ First we need to define a `package.json` for the extension that wants to use the
2422
}
2523
```
2624

27-
TODO insert example here
25+
The actual source code to get the active environment to run some script could look like this:
2826

2927
```typescript
30-
TODO
28+
// Import the API
29+
import { PythonExtension } from '@vscode/python-extension';
30+
31+
// Load the Python extension API
32+
const pythonApi: PythonExtension = await PythonExtension.api();
33+
34+
// This will return something like /usr/bin/python
35+
const environmentPath = pythonApi.environments.getActiveEnvironmentPath();
36+
37+
// `environmentPath.path` carries the value of the setting. Note that this path may point to a folder and not the
38+
// python binary. Depends entirely on how the env was created.
39+
// E.g., `conda create -n myenv python` ensures the env has a python binary
40+
// `conda create -n myenv` does not include a python binary.
41+
// Also, the path specified may not be valid, use the following to get complete details for this environment if
42+
// need be.
43+
44+
const environment = await pythonApi.environments.resolveEnvironment(environmentPath);
45+
if (environment) {
46+
// run your script here.
47+
}
3148
```
49+
50+
Check out [the wiki](https://aka.ms/pythonEnvironmentApi) for many more examples and usage.

pythonExtensionApi/example/todo.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)