Trigger Child Processes with a Command #45
-
I’m trying to run a python script when a certain command is run. In an attempt to perform a proof of concept I wrote a simple script (below). However, the python version is not displayed; I first wrote this in an empty typescript writing to the console not an |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @VaniSachdev , Your source code seems correct, and I wonder if I did a quick sample extension and it works as expected, as you can see in this print. Maybe you could add This is the full source of the extension, initially created with // The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import { spawn } from "child_process";
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "vscode-spawn" is now active!');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand('vscode-spawn.helloWorld', async () => {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
vscode.window.showInformationMessage('Hello World from vscode-spawn!');
const child = spawn("python3", ["--version"]);
child.stdout.on("data", (data) => {
console.log(`stdout: ${data}`);
vscode.window.showInformationMessage(`${data}`);
});
child.stderr.on("data", (data) => {
console.log(`stderr: ${data}`);
vscode.window.showErrorMessage(`Some error occured: ${data}`);
});
child.on("close", (code) => {
console.log(`child process exited with code ${code}`);
vscode.window.showInformationMessage("Closed...");
});
});
context.subscriptions.push(disposable);
}
// this method is called when your extension is deactivated
export function deactivate() {} Hope this helps |
Beta Was this translation helpful? Give feedback.
Hi @VaniSachdev ,
Your source code seems correct, and I wonder if
python
itself is properly configured and available in shell. Have you tried to runpython3 --version
inside the Terminal?I did a quick sample extension and it works as expected, as you can see in this print.
Maybe you could add
child.sdterr.on
to detect some error.This is the full source of the extension, initially created with
yo code