Skip to content

Commit fb80a37

Browse files
Add command to run Python Environment Tool (PET) in terminal (#608)
This PR adds a new command that allows users to run the Python Environment Tool (PET) binary in a terminal to search for Python installations interactively. ## Changes Made ### Command Registration - Added `python-envs.runPetInTerminal` command to `package.json` - Added localized title "Run Python Environment Tool (PET) in Terminal" to `package.nls.json` - Command is categorized under "Python" and only shown when the environments extension is enabled ### Implementation - Exported `getNativePythonToolsPath()` function from `nativePythonFinder.ts` - Added command handler in `extension.ts` that: - Gets the PET executable path using `getNativePythonToolsPath()` - Creates a new terminal named "Python Environment Tool (PET)" - Executes the PET binary in the terminal with proper path quoting - Shows the terminal to the user immediately - Includes comprehensive error handling with user-friendly messages ### Usage Users can now: 1. Open the Command Palette (`Ctrl+Shift+P` / `Cmd+Shift+P`) 2. Search for "Run Python Environment Tool (PET) in Terminal" 3. Execute the command to open a terminal running PET 4. Interactively explore and search for Python installations The implementation follows existing codebase patterns for terminal commands and maintains consistency with other extension features. Fixes #607. <!-- START COPILOT CODING AGENT TIPS --> --- 💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to start the survey. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: eleanorjboyd <[email protected]>
1 parent 4aef1c4 commit fb80a37

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@
262262
"title": "%python-envs.revealProjectInExplorer.title%",
263263
"category": "Python Envs",
264264
"icon": "$(folder-opened)"
265+
},
266+
{
267+
"command": "python-envs.runPetInTerminal",
268+
"title": "%python-envs.runPetInTerminal.title%",
269+
"category": "Python",
270+
"icon": "$(terminal)",
271+
"when": "config.python.useEnvironmentsExtension != false"
265272
}
266273
],
267274
"menus": {

package.nls.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@
3535
"python-envs.terminal.activate.title": "Activate Environment in Current Terminal",
3636
"python-envs.terminal.deactivate.title": "Deactivate Environment in Current Terminal",
3737
"python-envs.uninstallPackage.title": "Uninstall Package",
38-
"python-envs.revealProjectInExplorer.title": "Reveal Project in Explorer"
38+
"python-envs.revealProjectInExplorer.title": "Reveal Project in Explorer",
39+
"python-envs.runPetInTerminal.title": "Run Python Environment Tool (PET) in Terminal"
3940
}

src/extension.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { createDeferred } from './common/utils/deferred';
1212
import {
1313
activeTerminal,
1414
createLogOutputChannel,
15+
createTerminal,
1516
onDidChangeActiveTerminal,
1617
onDidChangeTerminalShellIntegration,
1718
} from './common/window.apis';
@@ -66,7 +67,11 @@ import { ProjectItem } from './features/views/treeViewItems';
6667
import { EnvironmentManagers, ProjectCreators, PythonProjectManager } from './internal.api';
6768
import { registerSystemPythonFeatures } from './managers/builtin/main';
6869
import { SysPythonManager } from './managers/builtin/sysPythonManager';
69-
import { createNativePythonFinder, NativePythonFinder } from './managers/common/nativePythonFinder';
70+
import {
71+
createNativePythonFinder,
72+
getNativePythonToolsPath,
73+
NativePythonFinder,
74+
} from './managers/common/nativePythonFinder';
7075
import { IDisposable } from './managers/common/types';
7176
import { registerCondaFeatures } from './managers/conda/main';
7277
import { registerPoetryFeatures } from './managers/poetry/main';
@@ -429,6 +434,20 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
429434
window.showErrorMessage(`Failed to open issue reporter: ${error}`);
430435
}
431436
}),
437+
commands.registerCommand('python-envs.runPetInTerminal', async () => {
438+
try {
439+
const petPath = await getNativePythonToolsPath();
440+
const terminal = createTerminal({
441+
name: 'Python Environment Tool (PET)',
442+
});
443+
terminal.show();
444+
terminal.sendText(`"${petPath}"`, true);
445+
traceInfo(`Running PET in terminal: ${petPath}`);
446+
} catch (error) {
447+
traceError('Error running PET in terminal', error);
448+
window.showErrorMessage(`Failed to run Python Environment Tool: ${error}`);
449+
}
450+
}),
432451
terminalActivation.onDidChangeTerminalActivationState(async (e) => {
433452
await setActivateMenuButtonContext(e.terminal, e.environment, e.activated);
434453
}),

src/managers/common/nativePythonFinder.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1+
import * as ch from 'child_process';
12
import * as fs from 'fs-extra';
23
import * as path from 'path';
4+
import { PassThrough } from 'stream';
5+
import { Disposable, ExtensionContext, LogOutputChannel, Uri } from 'vscode';
36
import * as rpc from 'vscode-jsonrpc/node';
4-
import * as ch from 'child_process';
7+
import { PythonProjectApi } from '../../api';
58
import { ENVS_EXTENSION_ID, PYTHON_EXTENSION_ID } from '../../common/constants';
69
import { getExtension } from '../../common/extension.apis';
7-
import { noop } from './utils';
8-
import { Disposable, ExtensionContext, LogOutputChannel, Uri } from 'vscode';
9-
import { PassThrough } from 'stream';
10-
import { PythonProjectApi } from '../../api';
11-
import { getConfiguration } from '../../common/workspace.apis';
12-
import { createRunningWorkerPool, WorkerPool } from '../../common/utils/workerPool';
1310
import { traceVerbose } from '../../common/logging';
14-
import { isWindows } from '../../common/utils/platformUtils';
1511
import { getUserHomeDir, untildify } from '../../common/utils/pathUtils';
12+
import { isWindows } from '../../common/utils/platformUtils';
13+
import { createRunningWorkerPool, WorkerPool } from '../../common/utils/workerPool';
14+
import { getConfiguration } from '../../common/workspace.apis';
15+
import { noop } from './utils';
1616

17-
async function getNativePythonToolsPath(): Promise<string> {
17+
export async function getNativePythonToolsPath(): Promise<string> {
1818
const envsExt = getExtension(ENVS_EXTENSION_ID);
1919
if (envsExt) {
2020
const petPath = path.join(envsExt.extensionPath, 'python-env-tools', 'bin', isWindows() ? 'pet.exe' : 'pet');

0 commit comments

Comments
 (0)