简体   繁体   中英

What is a VS Code Command to run a python file?

I have made an extension that opens a file dialog. What I would like to do is after the file is selected, I want a python file to run. What I need is the VS Code command to run a file (or perhaps a python file specifically?).

here is a working example where the command I use is a command that comments the selected line in the currently active editor. It works perfectly so I know this structure is generally correct. I just need the right command to replace the comment line command.

below is the code in questions with the working command I mentioned above. I found it using this resource: where I found the comment line command

// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const { ChildProcess } = require('child_process');
const vscode = require('vscode');
const { execFile } = require('node:child_process');
const { stdout, stderr } = require('process');

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed

/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {

    
    let disposable = vscode.commands.registerCommand('fileDialog.openFile', function () {

        const options = {
            canSelectMany: false,
            openLabel: 'Open'
       };
        vscode.window.showOpenDialog(options).then(fileUri => {
            if (fileUri && fileUri[0]) {
                console.log('Selected file: ' + fileUri[0].fsPath);
                vscode.commands.executeCommand('python.execInInterminal');  
            }
        });
    });

    context.subscriptions.push(disposable);
}

// this method is called when your extension is deactivated
function deactivate() {}

module.exports = {
    activate,
    deactivate
}

You can go for child_process' exec or spawn if you only need to run the Python script(s). If you really prefer to rely on the Python extension, then you'll need to at least feed the script's Uri into the executeCommand's argument - its the 2nd part of what you found .

function activate(context) {

    let disposable = vscode.commands.registerCommand('fileDialog.openFile', function () {

        const options = {
            canSelectMany: false,
            openLabel: 'Open',
            filters: {
                'Python script': ['py']
            }
        };
        vscode.window.showOpenDialog(options).then((fileUris) => {
            // This will always get an array of Uris regardless of canSelectMany being false
            fileUris?.forEach(async (fileUri) => {
                console.log(`Selected file:  ${fileUri.fsPath}`);
                await vscode.commands.executeCommand('python.execInInterminal', fileUri.fsPath);
            });
        });
    });

    context.subscriptions.push(disposable);
}

If you want to handle more things, you can refer to the thenable unittest code they included in the repo: https://github.com/microsoft/vscode-python/blob/main/src/test/smoke/runInTerminal.smoke.test.ts#L46-L48

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM