简体   繁体   English

什么是运行 python 文件的 VS 代码命令?

[英]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.我想做的是在选择文件后,我想要一个 python 文件运行。 What I need is the VS Code command to run a file (or perhaps a python file specifically?).我需要的是运行文件的 VS Code 命令(或者可能是 python 文件?)。

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我使用这个资源找到了它: 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).如果您只需要运行 Python 脚本,则可以 go 执行child_processspawn 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 .如果您真的更喜欢依赖 Python 扩展,那么您至少需要将脚本的 Uri 输入到 executeCommand 的参数中——它是您找到的第二部分。

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如果你想处理更多的事情,你可以参考他们包含在 repo 中的 thenable unittest 代码: https://github.com/microsoft/vscode-python/blob/main/src/test/smoke/runInTerminal.smoke。 test.ts#L46-L48

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 什么是 VS Code 中的 Prettier 键盘快捷键命令,用于仅格式化 React 代码块,而不仅仅是在文件自动保存时格式化? - What is Prettier keyboard shortcut command in VS Code to format only a block of React code, not just format on file autosave? 运行文件的heroku Scheduler命令是什么? - What is the heroku scheduler command to run a file? VS Code:Code Runner 扩展 - Python 下的运行按钮行为 - VS Code: Code Runner extension - Run button behaviour under Python 使用 VS Code 的 Code Runner 运行 js 文件时出现问题 - Problem using VS Code's Code Runner to run js file VS Code 在尝试运行活动文件时显示“权限被拒绝” - VS Code says "Permission denied" when trying to run active file 在 VS 代码中运行隔离的 JavaScript 命令(不是当前文件) - Run isolated JavaScript commands in VS Code (not current file) 为什么当我将 VS 代码中的代码文件保存为 .js 文件时,它不会在浏览器中运行或打开? - why is that when I save a code file in VS code as a .js file it wont run or open in a browser? 如何在 VS 代码中运行 JS - How to run JS in VS code 在里面运行python代码<script> tag of HTML file - Run python code inside <script> tag of HTML file 无法在 VS 代码中运行 javascript 代码 - Not able to run the javascript code in VS code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM