Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/chatSessions/vscode-node/copilotCLIPythonEnvironmentApi.ts
13399 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import {
7
Disposable,
8
Event,
9
FileChangeType,
10
LogOutputChannel,
11
MarkdownString,
12
TaskExecution,
13
Terminal,
14
TerminalOptions,
15
ThemeIcon,
16
Uri,
17
} from 'vscode';
18
19
/**
20
* The path to an icon, or a theme-specific configuration of icons.
21
*/
22
export type IconPath =
23
| Uri
24
| {
25
/**
26
* The icon path for the light theme.
27
*/
28
light: Uri;
29
/**
30
* The icon path for the dark theme.
31
*/
32
dark: Uri;
33
}
34
| ThemeIcon;
35
36
/**
37
* Options for executing a Python executable.
38
*/
39
export interface PythonCommandRunConfiguration {
40
/**
41
* Path to the binary like `python.exe` or `python3` to execute. This should be an absolute path
42
* to an executable that can be spawned.
43
*/
44
executable: string;
45
46
/**
47
* Arguments to pass to the python executable. These arguments will be passed on all execute calls.
48
* This is intended for cases where you might want to do interpreter specific flags.
49
*/
50
args?: string[];
51
}
52
53
/**
54
* Contains details on how to use a particular python environment
55
*
56
* Running In Terminal:
57
* 1. If {@link PythonEnvironmentExecutionInfo.activatedRun} is provided, then that will be used.
58
* 2. If {@link PythonEnvironmentExecutionInfo.activatedRun} is not provided, then:
59
* - If {@link PythonEnvironmentExecutionInfo.shellActivation} is provided and shell type is known, then that will be used.
60
* - If {@link PythonEnvironmentExecutionInfo.shellActivation} is provided and shell type is not known, then:
61
* - 'unknown' will be used if provided.
62
* - {@link PythonEnvironmentExecutionInfo.activation} will be used otherwise.
63
* - If {@link PythonEnvironmentExecutionInfo.shellActivation} is not provided, then {@link PythonEnvironmentExecutionInfo.activation} will be used.
64
* - If {@link PythonEnvironmentExecutionInfo.activation} is not provided, then {@link PythonEnvironmentExecutionInfo.run} will be used.
65
*
66
* Creating a Terminal:
67
* 1. If {@link PythonEnvironmentExecutionInfo.shellActivation} is provided and shell type is known, then that will be used.
68
* 2. If {@link PythonEnvironmentExecutionInfo.shellActivation} is provided and shell type is not known, then {@link PythonEnvironmentExecutionInfo.activation} will be used.
69
* 3. If {@link PythonEnvironmentExecutionInfo.shellActivation} is not provided, then:
70
* - 'unknown' will be used if provided.
71
* - {@link PythonEnvironmentExecutionInfo.activation} will be used otherwise.
72
* 4. If {@link PythonEnvironmentExecutionInfo.activation} is not provided, then {@link PythonEnvironmentExecutionInfo.run} will be used.
73
*
74
*/
75
export interface PythonEnvironmentExecutionInfo {
76
/**
77
* Details on how to run the python executable.
78
*/
79
run: PythonCommandRunConfiguration;
80
81
/**
82
* Details on how to run the python executable after activating the environment.
83
* If set this will overrides the {@link PythonEnvironmentExecutionInfo.run} command.
84
*/
85
activatedRun?: PythonCommandRunConfiguration;
86
87
/**
88
* Details on how to activate an environment.
89
*/
90
activation?: PythonCommandRunConfiguration[];
91
92
/**
93
* Details on how to activate an environment using a shell specific command.
94
* If set this will override the {@link PythonEnvironmentExecutionInfo.activation}.
95
* 'unknown' is used if shell type is not known.
96
* If 'unknown' is not provided and shell type is not known then
97
* {@link PythonEnvironmentExecutionInfo.activation} if set.
98
*/
99
shellActivation?: Map<string, PythonCommandRunConfiguration[]>;
100
101
/**
102
* Details on how to deactivate an environment.
103
*/
104
deactivation?: PythonCommandRunConfiguration[];
105
106
/**
107
* Details on how to deactivate an environment using a shell specific command.
108
* If set this will override the {@link PythonEnvironmentExecutionInfo.deactivation} property.
109
* 'unknown' is used if shell type is not known.
110
* If 'unknown' is not provided and shell type is not known then
111
* {@link PythonEnvironmentExecutionInfo.deactivation} if set.
112
*/
113
shellDeactivation?: Map<string, PythonCommandRunConfiguration[]>;
114
}
115
116
/**
117
* Interface representing the ID of a Python environment.
118
*/
119
export interface PythonEnvironmentId {
120
/**
121
* The unique identifier of the Python environment.
122
*/
123
id: string;
124
125
/**
126
* The ID of the manager responsible for the Python environment.
127
*/
128
managerId: string;
129
}
130
131
/**
132
* Display information for an environment group.
133
*/
134
export interface EnvironmentGroupInfo {
135
/**
136
* The name of the environment group. This is used as an identifier for the group.
137
*
138
* Note: The first instance of the group with the given name will be used in the UI.
139
*/
140
readonly name: string;
141
142
/**
143
* The description of the environment group.
144
*/
145
readonly description?: string;
146
147
/**
148
* The tooltip for the environment group, which can be a string or a Markdown string.
149
*/
150
readonly tooltip?: string | MarkdownString;
151
152
/**
153
* The icon path for the environment group, which can be a string, Uri, or an object with light and dark theme paths.
154
*/
155
readonly iconPath?: IconPath;
156
}
157
158
/**
159
* Interface representing information about a Python environment.
160
*/
161
export interface PythonEnvironmentInfo {
162
/**
163
* The name of the Python environment.
164
*/
165
readonly name: string;
166
167
/**
168
* The display name of the Python environment.
169
*/
170
readonly displayName: string;
171
172
/**
173
* The short display name of the Python environment.
174
*/
175
readonly shortDisplayName?: string;
176
177
/**
178
* The display path of the Python environment.
179
*/
180
readonly displayPath: string;
181
182
/**
183
* The version of the Python environment.
184
*/
185
readonly version: string;
186
187
/**
188
* Path to the python binary or environment folder.
189
*/
190
readonly environmentPath: Uri;
191
192
/**
193
* The description of the Python environment.
194
*/
195
readonly description?: string;
196
197
/**
198
* The tooltip for the Python environment, which can be a string or a Markdown string.
199
*/
200
readonly tooltip?: string | MarkdownString;
201
202
/**
203
* The icon path for the Python environment, which can be a string, Uri, or an object with light and dark theme paths.
204
*/
205
readonly iconPath?: IconPath;
206
207
/**
208
* Information on how to execute the Python environment. This is required for executing Python code in the environment.
209
*/
210
readonly execInfo: PythonEnvironmentExecutionInfo;
211
212
/**
213
* `sys.prefix` is the path to the base directory of the Python installation. Typically obtained by executing `sys.prefix` in the Python interpreter.
214
* This is required by extension like Jupyter, Pylance, and other extensions to provide better experience with python.
215
*/
216
readonly sysPrefix: string;
217
218
/**
219
* Optional `group` for this environment. This is used to group environments in the Environment Manager UI.
220
*/
221
readonly group?: string | EnvironmentGroupInfo;
222
}
223
224
/**
225
* Interface representing a Python environment.
226
*/
227
export interface PythonEnvironment extends PythonEnvironmentInfo {
228
/**
229
* The ID of the Python environment.
230
*/
231
readonly envId: PythonEnvironmentId;
232
}
233
234
/**
235
* Type representing the scope for setting a Python environment.
236
* Can be undefined or a URI.
237
*/
238
export type SetEnvironmentScope = undefined | Uri | Uri[];
239
240
/**
241
* Type representing the scope for getting a Python environment.
242
* Can be undefined or a URI.
243
*/
244
export type GetEnvironmentScope = undefined | Uri;
245
246
/**
247
* Type representing the scope for creating a Python environment.
248
* Can be a Python project or 'global'.
249
*/
250
export type CreateEnvironmentScope = Uri | Uri[] | 'global';
251
/**
252
* The scope for which environments are to be refreshed.
253
* - `undefined`: Search for environments globally and workspaces.
254
* - {@link Uri}: Environments in the workspace/folder or associated with the Uri.
255
*/
256
export type RefreshEnvironmentsScope = Uri | undefined;
257
258
/**
259
* The scope for which environments are required.
260
* - `"all"`: All environments.
261
* - `"global"`: Python installations that are usually a base for creating virtual environments.
262
* - {@link Uri}: Environments for the workspace/folder/file pointed to by the Uri.
263
*/
264
export type GetEnvironmentsScope = Uri | 'all' | 'global';
265
266
/**
267
* Event arguments for when the current Python environment changes.
268
*/
269
export type DidChangeEnvironmentEventArgs = {
270
/**
271
* The URI of the environment that changed.
272
*/
273
readonly uri: Uri | undefined;
274
275
/**
276
* The old Python environment before the change.
277
*/
278
readonly old: PythonEnvironment | undefined;
279
280
/**
281
* The new Python environment after the change.
282
*/
283
readonly new: PythonEnvironment | undefined;
284
};
285
286
/**
287
* Enum representing the kinds of environment changes.
288
*/
289
export enum EnvironmentChangeKind {
290
/**
291
* Indicates that an environment was added.
292
*/
293
add = 'add',
294
295
/**
296
* Indicates that an environment was removed.
297
*/
298
remove = 'remove',
299
}
300
301
/**
302
* Event arguments for when the list of Python environments changes.
303
*/
304
export type DidChangeEnvironmentsEventArgs = {
305
/**
306
* The kind of change that occurred (add or remove).
307
*/
308
kind: EnvironmentChangeKind;
309
310
/**
311
* The Python environment that was added or removed.
312
*/
313
environment: PythonEnvironment;
314
}[];
315
316
/**
317
* Type representing the context for resolving a Python environment.
318
*/
319
export type ResolveEnvironmentContext = Uri;
320
321
export interface QuickCreateConfig {
322
/**
323
* The description of the quick create step.
324
*/
325
readonly description: string;
326
327
/**
328
* The detail of the quick create step.
329
*/
330
readonly detail?: string;
331
}
332
333
/**
334
* Interface representing an environment manager.
335
*/
336
export interface EnvironmentManager {
337
/**
338
* The name of the environment manager. Allowed characters (a-z, A-Z, 0-9, -, _).
339
*/
340
readonly name: string;
341
342
/**
343
* The display name of the environment manager.
344
*/
345
readonly displayName?: string;
346
347
/**
348
* The preferred package manager ID for the environment manager. This is a combination
349
* of publisher id, extension id, and {@link EnvironmentManager.name package manager name}.
350
* `<publisher-id>.<extension-id>:<package-manager-name>`
351
*
352
* @example
353
* 'ms-python.python:pip'
354
*/
355
readonly preferredPackageManagerId: string;
356
357
/**
358
* The description of the environment manager.
359
*/
360
readonly description?: string;
361
362
/**
363
* The tooltip for the environment manager, which can be a string or a Markdown string.
364
*/
365
readonly tooltip?: string | MarkdownString | undefined;
366
367
/**
368
* The icon path for the environment manager, which can be a string, Uri, or an object with light and dark theme paths.
369
*/
370
readonly iconPath?: IconPath;
371
372
/**
373
* The log output channel for the environment manager.
374
*/
375
readonly log?: LogOutputChannel;
376
377
/**
378
* The quick create details for the environment manager. Having this method also enables the quick create feature
379
* for the environment manager. Should Implement {@link EnvironmentManager.create} to support quick create.
380
*/
381
quickCreateConfig?(): QuickCreateConfig | undefined;
382
383
/**
384
* Creates a new Python environment within the specified scope. Create should support adding a .gitignore file if it creates a folder within the workspace. If a manager does not support environment creation, do not implement this method; the UI disables "create" options when `this.manager.create === undefined`.
385
* @param scope - The scope within which to create the environment.
386
* @param options - Optional parameters for creating the Python environment.
387
* @returns A promise that resolves to the created Python environment, or undefined if creation failed.
388
*/
389
create?(scope: CreateEnvironmentScope, options?: CreateEnvironmentOptions): Promise<PythonEnvironment | undefined>;
390
391
/**
392
* Removes the specified Python environment.
393
* @param environment - The Python environment to remove.
394
* @returns A promise that resolves when the environment is removed.
395
*/
396
remove?(environment: PythonEnvironment): Promise<void>;
397
398
/**
399
* Refreshes the list of Python environments within the specified scope.
400
* @param scope - The scope within which to refresh environments.
401
* @returns A promise that resolves when the refresh is complete.
402
*/
403
refresh(scope: RefreshEnvironmentsScope): Promise<void>;
404
405
/**
406
* Retrieves a list of Python environments within the specified scope.
407
* @param scope - The scope within which to retrieve environments.
408
* @returns A promise that resolves to an array of Python environments.
409
*/
410
getEnvironments(scope: GetEnvironmentsScope): Promise<PythonEnvironment[]>;
411
412
/**
413
* Event that is fired when the list of Python environments changes.
414
*/
415
onDidChangeEnvironments?: Event<DidChangeEnvironmentsEventArgs>;
416
417
/**
418
* Sets the current Python environment within the specified scope.
419
* @param scope - The scope within which to set the environment.
420
* @param environment - The Python environment to set. If undefined, the environment is unset.
421
* @returns A promise that resolves when the environment is set.
422
*/
423
set(scope: SetEnvironmentScope, environment?: PythonEnvironment): Promise<void>;
424
425
/**
426
* Retrieves the current Python environment within the specified scope.
427
* @param scope - The scope within which to retrieve the environment.
428
* @returns A promise that resolves to the current Python environment, or undefined if none is set.
429
*/
430
get(scope: GetEnvironmentScope): Promise<PythonEnvironment | undefined>;
431
432
/**
433
* Event that is fired when the current Python environment changes.
434
*/
435
onDidChangeEnvironment?: Event<DidChangeEnvironmentEventArgs>;
436
437
/**
438
* Resolves the specified Python environment. The environment can be either a {@link PythonEnvironment} or a {@link Uri} context.
439
*
440
* This method is used to obtain a fully detailed {@link PythonEnvironment} object. The input can be:
441
* - A {@link PythonEnvironment} object, which might be missing key details such as {@link PythonEnvironment.execInfo}.
442
* - A {@link Uri} object, which typically represents either:
443
* - A folder that contains the Python environment.
444
* - The path to a Python executable.
445
*
446
* @param context - The context for resolving the environment, which can be a {@link PythonEnvironment} or a {@link Uri}.
447
* @returns A promise that resolves to the fully detailed {@link PythonEnvironment}, or `undefined` if the environment cannot be resolved.
448
*/
449
resolve(context: ResolveEnvironmentContext): Promise<PythonEnvironment | undefined>;
450
451
/**
452
* Clears the environment manager's cache.
453
*
454
* @returns A promise that resolves when the cache is cleared.
455
*/
456
clearCache?(): Promise<void>;
457
}
458
459
/**
460
* Interface representing a package ID.
461
*/
462
export interface PackageId {
463
/**
464
* The ID of the package.
465
*/
466
id: string;
467
468
/**
469
* The ID of the package manager.
470
*/
471
managerId: string;
472
473
/**
474
* The ID of the environment in which the package is installed.
475
*/
476
environmentId: string;
477
}
478
479
/**
480
* Interface representing package information.
481
*/
482
export interface PackageInfo {
483
/**
484
* The name of the package.
485
*/
486
readonly name: string;
487
488
/**
489
* The display name of the package.
490
*/
491
readonly displayName: string;
492
493
/**
494
* The version of the package.
495
*/
496
readonly version?: string;
497
498
/**
499
* The description of the package.
500
*/
501
readonly description?: string;
502
503
/**
504
* The tooltip for the package, which can be a string or a Markdown string.
505
*/
506
readonly tooltip?: string | MarkdownString | undefined;
507
508
/**
509
* The icon path for the package, which can be a string, Uri, or an object with light and dark theme paths.
510
*/
511
readonly iconPath?: IconPath;
512
513
/**
514
* The URIs associated with the package.
515
*/
516
readonly uris?: readonly Uri[];
517
}
518
519
/**
520
* Interface representing a package.
521
*/
522
export interface Package extends PackageInfo {
523
/**
524
* The ID of the package.
525
*/
526
readonly pkgId: PackageId;
527
}
528
529
/**
530
* Enum representing the kinds of package changes.
531
*/
532
export enum PackageChangeKind {
533
/**
534
* Indicates that a package was added.
535
*/
536
add = 'add',
537
538
/**
539
* Indicates that a package was removed.
540
*/
541
remove = 'remove',
542
}
543
544
/**
545
* Event arguments for when packages change.
546
*/
547
export interface DidChangePackagesEventArgs {
548
/**
549
* The Python environment in which the packages changed.
550
*/
551
environment: PythonEnvironment;
552
553
/**
554
* The package manager responsible for the changes.
555
*/
556
manager: PackageManager;
557
558
/**
559
* The list of changes, each containing the kind of change and the package affected.
560
*/
561
changes: { kind: PackageChangeKind; pkg: Package }[];
562
}
563
564
/**
565
* Interface representing a package manager.
566
*/
567
export interface PackageManager {
568
/**
569
* The name of the package manager. Allowed characters (a-z, A-Z, 0-9, -, _).
570
*/
571
name: string;
572
573
/**
574
* The display name of the package manager.
575
*/
576
displayName?: string;
577
578
/**
579
* The description of the package manager.
580
*/
581
description?: string;
582
583
/**
584
* The tooltip for the package manager, which can be a string or a Markdown string.
585
*/
586
tooltip?: string | MarkdownString | undefined;
587
588
/**
589
* The icon path for the package manager, which can be a string, Uri, or an object with light and dark theme paths.
590
*/
591
iconPath?: IconPath;
592
593
/**
594
* The log output channel for the package manager.
595
*/
596
log?: LogOutputChannel;
597
598
/**
599
* Installs/Uninstall packages in the specified Python environment.
600
* @param environment - The Python environment in which to install packages.
601
* @param options - Options for managing packages.
602
* @returns A promise that resolves when the installation is complete.
603
*/
604
manage(environment: PythonEnvironment, options: PackageManagementOptions): Promise<void>;
605
606
/**
607
* Refreshes the package list for the specified Python environment.
608
* @param environment - The Python environment for which to refresh the package list.
609
* @returns A promise that resolves when the refresh is complete.
610
*/
611
refresh(environment: PythonEnvironment): Promise<void>;
612
613
/**
614
* Retrieves the list of packages for the specified Python environment.
615
* @param environment - The Python environment for which to retrieve packages.
616
* @returns An array of packages, or undefined if the packages could not be retrieved.
617
*/
618
getPackages(environment: PythonEnvironment): Promise<Package[] | undefined>;
619
620
/**
621
* Event that is fired when packages change.
622
*/
623
onDidChangePackages?: Event<DidChangePackagesEventArgs>;
624
625
/**
626
* Clears the package manager's cache.
627
* @returns A promise that resolves when the cache is cleared.
628
*/
629
clearCache?(): Promise<void>;
630
}
631
632
/**
633
* Interface representing a Python project.
634
*/
635
export interface PythonProject {
636
/**
637
* The name of the Python project.
638
*/
639
readonly name: string;
640
641
/**
642
* The URI of the Python project.
643
*/
644
readonly uri: Uri;
645
646
/**
647
* The description of the Python project.
648
*/
649
readonly description?: string;
650
651
/**
652
* The tooltip for the Python project, which can be a string or a Markdown string.
653
*/
654
readonly tooltip?: string | MarkdownString;
655
}
656
657
/**
658
* Options for creating a Python project.
659
*/
660
export interface PythonProjectCreatorOptions {
661
/**
662
* The name of the Python project.
663
*/
664
name: string;
665
666
/**
667
* Path provided as the root for the project.
668
*/
669
rootUri: Uri;
670
671
/**
672
* Boolean indicating whether the project should be created without any user input.
673
*/
674
quickCreate?: boolean;
675
}
676
677
/**
678
* Interface representing a creator for Python projects.
679
*/
680
export interface PythonProjectCreator {
681
/**
682
* The name of the Python project creator.
683
*/
684
readonly name: string;
685
686
/**
687
* The display name of the Python project creator.
688
*/
689
readonly displayName?: string;
690
691
/**
692
* The description of the Python project creator.
693
*/
694
readonly description?: string;
695
696
/**
697
* The tooltip for the Python project creator, which can be a string or a Markdown string.
698
*/
699
readonly tooltip?: string | MarkdownString;
700
701
/**
702
* Creates a new Python project(s) or, if files are not a project, returns Uri(s) to the created files.
703
* Anything that needs its own python environment constitutes a project.
704
* @param options Optional parameters for creating the Python project.
705
* @returns A promise that resolves to one of the following:
706
* - PythonProject or PythonProject[]: when a single or multiple projects are created.
707
* - Uri or Uri[]: when files are created that do not constitute a project.
708
* - undefined: if project creation fails.
709
*/
710
create(options?: PythonProjectCreatorOptions): Promise<PythonProject | PythonProject[] | Uri | Uri[] | undefined>;
711
712
/**
713
* A flag indicating whether the project creator supports quick create where no user input is required.
714
*/
715
readonly supportsQuickCreate?: boolean;
716
}
717
718
/**
719
* Event arguments for when Python projects change.
720
*/
721
export interface DidChangePythonProjectsEventArgs {
722
/**
723
* The list of Python projects that were added.
724
*/
725
added: PythonProject[];
726
727
/**
728
* The list of Python projects that were removed.
729
*/
730
removed: PythonProject[];
731
}
732
733
export type PackageManagementOptions =
734
| {
735
/**
736
* Upgrade the packages if they are already installed.
737
*/
738
upgrade?: boolean;
739
740
/**
741
* Show option to skip package installation or uninstallation.
742
*/
743
showSkipOption?: boolean;
744
/**
745
* The list of packages to install.
746
*/
747
install: string[];
748
749
/**
750
* The list of packages to uninstall.
751
*/
752
uninstall?: string[];
753
}
754
| {
755
/**
756
* Upgrade the packages if they are already installed.
757
*/
758
upgrade?: boolean;
759
760
/**
761
* Show option to skip package installation or uninstallation.
762
*/
763
showSkipOption?: boolean;
764
/**
765
* The list of packages to install.
766
*/
767
install?: string[];
768
769
/**
770
* The list of packages to uninstall.
771
*/
772
uninstall: string[];
773
};
774
775
/**
776
* Options for creating a Python environment.
777
*/
778
export interface CreateEnvironmentOptions {
779
/**
780
* Provides some context about quick create based on user input.
781
* - if true, the environment should be created without any user input or prompts.
782
* - if false, the environment creation can show user input or prompts.
783
* This also means user explicitly skipped the quick create option.
784
* - if undefined, the environment creation can show user input or prompts.
785
* You can show quick create option to the user if you support it.
786
*/
787
quickCreate?: boolean;
788
/**
789
* Packages to install in addition to the automatically picked packages as a part of creating environment.
790
*/
791
additionalPackages?: string[];
792
}
793
794
/**
795
* Object representing the process started using run in background API.
796
*/
797
export interface PythonProcess {
798
/**
799
* The process ID of the Python process.
800
*/
801
readonly pid?: number;
802
803
/**
804
* The standard input of the Python process.
805
*/
806
readonly stdin: NodeJS.WritableStream;
807
808
/**
809
* The standard output of the Python process.
810
*/
811
readonly stdout: NodeJS.ReadableStream;
812
813
/**
814
* The standard error of the Python process.
815
*/
816
readonly stderr: NodeJS.ReadableStream;
817
818
/**
819
* Kills the Python process.
820
*/
821
kill(): void;
822
823
/**
824
* Event that is fired when the Python process exits.
825
*/
826
onExit(listener: (code: number | null, signal: NodeJS.Signals | null) => void): void;
827
}
828
829
export interface PythonEnvironmentManagerRegistrationApi {
830
/**
831
* Register an environment manager implementation.
832
*
833
* @param manager Environment Manager implementation to register.
834
* @returns A disposable that can be used to unregister the environment manager.
835
* @see {@link EnvironmentManager}
836
*/
837
registerEnvironmentManager(manager: EnvironmentManager): Disposable;
838
}
839
840
export interface PythonEnvironmentItemApi {
841
/**
842
* Create a Python environment item from the provided environment info. This item is used to interact
843
* with the environment.
844
*
845
* @param info Some details about the environment like name, version, etc. needed to interact with the environment.
846
* @param manager The environment manager to associate with the environment.
847
* @returns The Python environment.
848
*/
849
createPythonEnvironmentItem(info: PythonEnvironmentInfo, manager: EnvironmentManager): PythonEnvironment;
850
}
851
852
export interface PythonEnvironmentManagementApi {
853
/**
854
* Create a Python environment using environment manager associated with the scope.
855
*
856
* @param scope Where the environment is to be created.
857
* @param options Optional parameters for creating the Python environment.
858
* @returns The Python environment created. `undefined` if not created.
859
*/
860
createEnvironment(
861
scope: CreateEnvironmentScope,
862
options?: CreateEnvironmentOptions,
863
): Promise<PythonEnvironment | undefined>;
864
865
/**
866
* Remove a Python environment.
867
*
868
* @param environment The Python environment to remove.
869
* @returns A promise that resolves when the environment has been removed.
870
*/
871
removeEnvironment(environment: PythonEnvironment): Promise<void>;
872
}
873
874
export interface PythonEnvironmentsApi {
875
/**
876
* Initiates a refresh of Python environments within the specified scope.
877
* @param scope - The scope within which to search for environments.
878
* @returns A promise that resolves when the search is complete.
879
*/
880
refreshEnvironments(scope: RefreshEnvironmentsScope): Promise<void>;
881
882
/**
883
* Retrieves a list of Python environments within the specified scope.
884
* @param scope - The scope within which to retrieve environments.
885
* @returns A promise that resolves to an array of Python environments.
886
*/
887
getEnvironments(scope: GetEnvironmentsScope): Promise<PythonEnvironment[]>;
888
889
/**
890
* Event that is fired when the list of Python environments changes.
891
* @see {@link DidChangeEnvironmentsEventArgs}
892
*/
893
onDidChangeEnvironments: Event<DidChangeEnvironmentsEventArgs>;
894
895
/**
896
* This method is used to get the details missing from a PythonEnvironment. Like
897
* {@link PythonEnvironment.execInfo} and other details.
898
*
899
* @param context : The PythonEnvironment or Uri for which details are required.
900
*/
901
resolveEnvironment(context: ResolveEnvironmentContext): Promise<PythonEnvironment | undefined>;
902
}
903
904
export interface PythonProjectEnvironmentApi {
905
/**
906
* Sets the current Python environment within the specified scope.
907
* @param scope - The scope within which to set the environment.
908
* @param environment - The Python environment to set. If undefined, the environment is unset.
909
*/
910
setEnvironment(scope: SetEnvironmentScope, environment?: PythonEnvironment): Promise<void>;
911
912
/**
913
* Retrieves the current Python environment within the specified scope.
914
* @param scope - The scope within which to retrieve the environment.
915
* @returns A promise that resolves to the current Python environment, or undefined if none is set.
916
*/
917
getEnvironment(scope: GetEnvironmentScope): Promise<PythonEnvironment | undefined>;
918
919
/**
920
* Event that is fired when the selected Python environment changes for Project, Folder or File.
921
* @see {@link DidChangeEnvironmentEventArgs}
922
*/
923
onDidChangeEnvironment: Event<DidChangeEnvironmentEventArgs>;
924
}
925
926
export interface PythonEnvironmentManagerApi
927
extends PythonEnvironmentManagerRegistrationApi,
928
PythonEnvironmentItemApi,
929
PythonEnvironmentManagementApi,
930
PythonEnvironmentsApi,
931
PythonProjectEnvironmentApi { }
932
933
export interface PythonPackageManagerRegistrationApi {
934
/**
935
* Register a package manager implementation.
936
*
937
* @param manager Package Manager implementation to register.
938
* @returns A disposable that can be used to unregister the package manager.
939
* @see {@link PackageManager}
940
*/
941
registerPackageManager(manager: PackageManager): Disposable;
942
}
943
944
export interface PythonPackageGetterApi {
945
/**
946
* Refresh the list of packages in a Python Environment.
947
*
948
* @param environment The Python Environment for which the list of packages is to be refreshed.
949
* @returns A promise that resolves when the list of packages has been refreshed.
950
*/
951
refreshPackages(environment: PythonEnvironment): Promise<void>;
952
953
/**
954
* Get the list of packages in a Python Environment.
955
*
956
* @param environment The Python Environment for which the list of packages is required.
957
* @returns The list of packages in the Python Environment.
958
*/
959
getPackages(environment: PythonEnvironment): Promise<Package[] | undefined>;
960
961
/**
962
* Event raised when the list of packages in a Python Environment changes.
963
* @see {@link DidChangePackagesEventArgs}
964
*/
965
onDidChangePackages: Event<DidChangePackagesEventArgs>;
966
}
967
968
export interface PythonPackageItemApi {
969
/**
970
* Create a package item from the provided package info.
971
*
972
* @param info The package info.
973
* @param environment The Python Environment in which the package is installed.
974
* @param manager The package manager that installed the package.
975
* @returns The package item.
976
*/
977
createPackageItem(info: PackageInfo, environment: PythonEnvironment, manager: PackageManager): Package;
978
}
979
980
export interface PythonPackageManagementApi {
981
/**
982
* Install/Uninstall packages into a Python Environment.
983
*
984
* @param environment The Python Environment into which packages are to be installed.
985
* @param packages The packages to install.
986
* @param options Options for installing packages.
987
*/
988
managePackages(environment: PythonEnvironment, options: PackageManagementOptions): Promise<void>;
989
}
990
991
export interface PythonPackageManagerApi
992
extends PythonPackageManagerRegistrationApi,
993
PythonPackageGetterApi,
994
PythonPackageManagementApi,
995
PythonPackageItemApi { }
996
997
export interface PythonProjectCreationApi {
998
/**
999
* Register a Python project creator.
1000
*
1001
* @param creator The project creator to register.
1002
* @returns A disposable that can be used to unregister the project creator.
1003
* @see {@link PythonProjectCreator}
1004
*/
1005
registerPythonProjectCreator(creator: PythonProjectCreator): Disposable;
1006
}
1007
export interface PythonProjectGetterApi {
1008
/**
1009
* Get all python projects.
1010
*/
1011
getPythonProjects(): readonly PythonProject[];
1012
1013
/**
1014
* Get the python project for a given URI.
1015
*
1016
* @param uri The URI of the project
1017
* @returns The project or `undefined` if not found.
1018
*/
1019
getPythonProject(uri: Uri): PythonProject | undefined;
1020
}
1021
1022
export interface PythonProjectModifyApi {
1023
/**
1024
* Add a python project or projects to the list of projects.
1025
*
1026
* @param projects The project or projects to add.
1027
*/
1028
addPythonProject(projects: PythonProject | PythonProject[]): void;
1029
1030
/**
1031
* Remove a python project from the list of projects.
1032
*
1033
* @param project The project to remove.
1034
*/
1035
removePythonProject(project: PythonProject): void;
1036
1037
/**
1038
* Event raised when python projects are added or removed.
1039
* @see {@link DidChangePythonProjectsEventArgs}
1040
*/
1041
onDidChangePythonProjects: Event<DidChangePythonProjectsEventArgs>;
1042
}
1043
1044
/**
1045
* The API for interacting with Python projects. A project in python is any folder or file that is a contained
1046
* in some manner. For example, a PEP-723 compliant file can be treated as a project. A folder with a `pyproject.toml`,
1047
* or just python files can be treated as a project. All this allows you to do is set a python environment for that project.
1048
*
1049
* By default all `vscode.workspace.workspaceFolders` are treated as projects.
1050
*/
1051
export interface PythonProjectApi extends PythonProjectCreationApi, PythonProjectGetterApi, PythonProjectModifyApi { }
1052
1053
export interface PythonTerminalCreateOptions extends TerminalOptions {
1054
/**
1055
* Whether to disable activation on create.
1056
*/
1057
disableActivation?: boolean;
1058
}
1059
1060
export interface PythonTerminalCreateApi {
1061
/**
1062
* Creates a terminal and activates any (activatable) environment for the terminal.
1063
*
1064
* @param environment The Python environment to activate.
1065
* @param options Options for creating the terminal.
1066
*
1067
* Note: Non-activatable environments have no effect on the terminal.
1068
*/
1069
createTerminal(environment: PythonEnvironment, options: PythonTerminalCreateOptions): Promise<Terminal>;
1070
}
1071
1072
/**
1073
* Options for running a Python script or module in a terminal.
1074
*
1075
* Example:
1076
* * Running Script: `python myscript.py --arg1`
1077
* ```typescript
1078
* {
1079
* args: ["myscript.py", "--arg1"]
1080
* }
1081
* ```
1082
* * Running a module: `python -m my_module --arg1`
1083
* ```typescript
1084
* {
1085
* args: ["-m", "my_module", "--arg1"]
1086
* }
1087
* ```
1088
*/
1089
export interface PythonTerminalExecutionOptions {
1090
/**
1091
* Current working directory for the terminal. This in only used to create the terminal.
1092
*/
1093
cwd: string | Uri;
1094
1095
/**
1096
* Arguments to pass to the python executable.
1097
*/
1098
args?: string[];
1099
1100
/**
1101
* Set `true` to show the terminal.
1102
*/
1103
show?: boolean;
1104
}
1105
1106
export interface PythonTerminalRunApi {
1107
/**
1108
* Runs a Python script or module in a terminal. This API will create a terminal if one is not available to use.
1109
* If a terminal is available, it will be used to run the script or module.
1110
*
1111
* Note:
1112
* - If you restart VS Code, this will create a new terminal, this is a limitation of VS Code.
1113
* - If you close the terminal, this will create a new terminal.
1114
* - In cases of multi-root/project scenario, it will create a separate terminal for each project.
1115
*/
1116
runInTerminal(environment: PythonEnvironment, options: PythonTerminalExecutionOptions): Promise<Terminal>;
1117
1118
/**
1119
* Runs a Python script or module in a dedicated terminal. This API will create a terminal if one is not available to use.
1120
* If a terminal is available, it will be used to run the script or module. This terminal will be dedicated to the script,
1121
* and selected based on the `terminalKey`.
1122
*
1123
* @param terminalKey A unique key to identify the terminal. For scripts you can use the Uri of the script file.
1124
*/
1125
runInDedicatedTerminal(
1126
terminalKey: Uri | string,
1127
environment: PythonEnvironment,
1128
options: PythonTerminalExecutionOptions,
1129
): Promise<Terminal>;
1130
}
1131
1132
/**
1133
* Options for running a Python task.
1134
*
1135
* Example:
1136
* * Running Script: `python myscript.py --arg1`
1137
* ```typescript
1138
* {
1139
* args: ["myscript.py", "--arg1"]
1140
* }
1141
* ```
1142
* * Running a module: `python -m my_module --arg1`
1143
* ```typescript
1144
* {
1145
* args: ["-m", "my_module", "--arg1"]
1146
* }
1147
* ```
1148
*/
1149
export interface PythonTaskExecutionOptions {
1150
/**
1151
* Name of the task to run.
1152
*/
1153
name: string;
1154
1155
/**
1156
* Arguments to pass to the python executable.
1157
*/
1158
args: string[];
1159
1160
/**
1161
* The Python project to use for the task.
1162
*/
1163
project?: PythonProject;
1164
1165
/**
1166
* Current working directory for the task. Default is the project directory for the script being run.
1167
*/
1168
cwd?: string;
1169
1170
/**
1171
* Environment variables to set for the task.
1172
*/
1173
env?: { [key: string]: string };
1174
}
1175
1176
export interface PythonTaskRunApi {
1177
/**
1178
* Run a Python script or module as a task.
1179
*
1180
*/
1181
runAsTask(environment: PythonEnvironment, options: PythonTaskExecutionOptions): Promise<TaskExecution>;
1182
}
1183
1184
/**
1185
* Options for running a Python script or module in the background.
1186
*/
1187
export interface PythonBackgroundRunOptions {
1188
/**
1189
* The Python environment to use for running the script or module.
1190
*/
1191
args: string[];
1192
1193
/**
1194
* Current working directory for the script or module. Default is the project directory for the script being run.
1195
*/
1196
cwd?: string;
1197
1198
/**
1199
* Environment variables to set for the script or module.
1200
*/
1201
env?: { [key: string]: string | undefined };
1202
}
1203
export interface PythonBackgroundRunApi {
1204
/**
1205
* Run a Python script or module in the background. This API will create a new process to run the script or module.
1206
*/
1207
runInBackground(environment: PythonEnvironment, options: PythonBackgroundRunOptions): Promise<PythonProcess>;
1208
}
1209
1210
export interface PythonExecutionApi
1211
extends PythonTerminalCreateApi,
1212
PythonTerminalRunApi,
1213
PythonTaskRunApi,
1214
PythonBackgroundRunApi { }
1215
1216
/**
1217
* Event arguments for when the monitored `.env` files or any other sources change.
1218
*/
1219
export interface DidChangeEnvironmentVariablesEventArgs {
1220
/**
1221
* The URI of the file that changed. No `Uri` means a non-file source of environment variables changed.
1222
*/
1223
uri?: Uri;
1224
1225
/**
1226
* The type of change that occurred.
1227
*/
1228
changeType: FileChangeType;
1229
}
1230
1231
export interface PythonEnvironmentVariablesApi {
1232
/**
1233
* Get environment variables for a workspace. This picks up `.env` file from the root of the
1234
* workspace.
1235
*
1236
* Order of overrides:
1237
* 1. `baseEnvVar` if given or `process.env`
1238
* 2. `.env` file from the "python.envFile" setting in the workspace.
1239
* 3. `.env` file at the root of the python project.
1240
* 4. `overrides` in the order provided.
1241
*
1242
* @param uri The URI of the project, workspace or a file in a for which environment variables are required.If not provided,
1243
* it fetches the environment variables for the global scope.
1244
* @param overrides Additional environment variables to override the defaults.
1245
* @param baseEnvVar The base environment variables that should be used as a starting point.
1246
*/
1247
getEnvironmentVariables(
1248
uri: Uri | undefined,
1249
overrides?: ({ [key: string]: string | undefined } | Uri)[],
1250
baseEnvVar?: { [key: string]: string | undefined },
1251
): Promise<{ [key: string]: string | undefined }>;
1252
1253
/**
1254
* Event raised when `.env` file changes or any other monitored source of env variable changes.
1255
*/
1256
onDidChangeEnvironmentVariables: Event<DidChangeEnvironmentVariablesEventArgs>;
1257
}
1258
1259
/**
1260
* The API for interacting with Python environments, package managers, and projects.
1261
*/
1262
export interface PythonEnvironmentApi
1263
extends PythonEnvironmentManagerApi,
1264
PythonPackageManagerApi,
1265
PythonProjectApi,
1266
PythonExecutionApi,
1267
PythonEnvironmentVariablesApi { }
1268
1269