如何配置在VS Code中编辑器选项卡旁边的“运行”按钮,用于Python文件?

huangapple go评论68阅读模式
英文:

How can I configure the "Run" button next to the editor tabs in VS Code for Python files?

问题

我在我的 launch.json 中有以下配置:

{
    "configurations": 
    [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "cwd": "${fileDirname}",
            "env": {"PYTHONPATH": "C:/repo/python"}
        }
    ]
}

有了这个配置,我可以轻松地从运行/调试侧边栏运行和调试当前脚本:

如何配置在VS Code中编辑器选项卡旁边的“运行”按钮,用于Python文件?

到目前为止一切都很好。但现在我也想能够直接从编辑器选项卡上的“运行”按钮运行脚本:

如何配置在VS Code中编辑器选项卡旁边的“运行”按钮,用于Python文件?

不幸的是,这并不起作用。它运行文件,但上面的配置不起作用。

我如何为编辑器选项卡上的“运行”按钮添加带有 PYTHONPATH 等配置?

英文:

I have the following configuration in my launch.json:

<!-- language: lang-json -->

{
    &quot;configurations&quot;: 
    [
    {
      &quot;name&quot;: &quot;Python: Current File&quot;,
      &quot;type&quot;: &quot;python&quot;,
      &quot;request&quot;: &quot;launch&quot;,
      &quot;program&quot;: &quot;${file}&quot;,
      &quot;console&quot;: &quot;integratedTerminal&quot;,
      &quot;justMyCode&quot;: true,
      &quot;cwd&quot;: &quot;${fileDirname}&quot;,
      &quot;env&quot;: {&quot;PYTHONPATH&quot;: &quot;C:/repo/python&quot;}
    }
    ]
}

With this, I can easily run and debug the current script from the Run/Debug side panel:

如何配置在VS Code中编辑器选项卡旁边的“运行”按钮,用于Python文件?

All good, so far. But now I'd like to also be able to directly run the script from the Run button on the editor tabs:

如何配置在VS Code中编辑器选项卡旁边的“运行”按钮,用于Python文件?

Unfortunately, this does not work. It runs the file, but the configuration from above is not taken.

How can I add a configuration with PYTHONPATH etc. also for this Run button in the editor tabs?

答案1

得分: 1

请注意,launch.json 仅用于调试。编辑器界面右上角的三角形按钮仅运行或调试当前脚本文件,并不应用于 launch 中的配置。

如果您想要在 Run and Debug 面板之外调试文件并使用 launch.json,可以使用快捷键 Ctrl+F5 或顶部菜单 Run-->Run Without Debugging

关于您提到的右上角播放按钮的配置,这已经存在于GitHub上,但尚未收集足够的投票。如果您对此感兴趣,可以继续提出建议并关注。

英文:

Note that launch.json only works for debugging. The triangle button in the upper right corner of the editor interface only runs or debugs the current script file, and does not apply the configuration in launch.

If you want to debug the file outside the Run and Debug panel and use launch.json, use the shortcut key <kbd>Ctrl</kbd>+<kbd>F5</kbd> or the top menu Run--&gt;Run Without Debugging.

如何配置在VS Code中编辑器选项卡旁边的“运行”按钮,用于Python文件?

As for the configuration of the play button in the upper right corner you mentioned, this has existed on GitHub, but not enough votes have been collected. If you are interested in this, you can continue to raise it and pay attention.

答案2

得分: 0

我相当确信"Run Python File"按钮调用的是命令面板中的"Python: Run Python File in Terminal"命令,如果我理解正确的话,它使用以"python.terminal.*"开头的设置。在我写这篇文章时,这些设置的当前列表如下(显示它们的默认值):

// 在加载扩展时在当前终端中激活 Python 环境。
"python.terminal.activateEnvInCurrentTerminal": false,

// 在创建的所有终端中激活 Python 环境。
"python.terminal.activateEnvironment": true,

// 在终端中执行文件时,是否使用文件的目录而不是当前打开的文件夹。
"python.terminal.executeInFileDir": false,

// 启动 Python 终端时,是否将光标焦点放在终端上。
"python.terminal.focusAfterLaunch": false,

// 在终端中执行文件时要使用的 Python 启动参数。
"python.terminal.launchArgs": [],

注意:关于调试 Python 的文档可以在这里找到:https://code.visualstudio.com/docs/python/debugging。

在 Python 类型的启动配置中,有一个名为"purpose"的属性,您可以将"debug-in-terminal"放在数组值中,这似乎使启动配置在使用屏幕右上角的"debug in terminal"模式时生效。

对于命令行参数,请使用"python.terminal.launchArgs"设置。至于"PYTHONPATH",这是一个环境变量,因此您需要修改"terminal.integrated.env.windows"、"terminal.integrated.env.osx"和"terminal.integrated.env.linux"中的每个设置.json 文件中的设置.json 文件(您可以在工作区或用户的设置.json 文件中执行此操作)。例如:

"terminal.integrated.env.linux": {
    "FOO": "foo",
    "BAR": "hello",
    "BAZ": "baz",
},
// 重复为 OSX 和 Windows(不幸的是,这很重复)

还要注意的是,由于"Python: Run Python File in Terminal"功能尝试重用终端,并且据我所知,您无法在创建后更改进程的环境变量,因此您需要关闭终端以使您对"terminal.integrated.env.*"所做的更改生效(从重新创建终端以及随之的 shell 进程中获取更改)。

注意:有一个相关的问题记录在:Allow "Run Python File in Terminal" button to be configurable #11812

英文:

I'm pretty sure the Run Python File button invokes the same thing as the Python: Run Python File in Terminal command in the command palette, which- if I understand correctly- uses settings that start with python.terminal.*. The current list of those settings at the time of this writing is the following (shown with their default values):

// Activate Python Environment in the current Terminal on load of the Extension.
&quot;python.terminal.activateEnvInCurrentTerminal&quot;: false,

// Activate Python Environment in all Terminals created.
&quot;python.terminal.activateEnvironment&quot;: true,

// When executing a file in the terminal, whether to use execute in the file&#39;s directory, instead of the current open folder.
&quot;python.terminal.executeInFileDir&quot;: false,

// When launching a python terminal, whether to focus the cursor on the terminal.
&quot;python.terminal.focusAfterLaunch&quot;: false,

// Python launch arguments to use when executing a file in the terminal.
&quot;python.terminal.launchArgs&quot;: [],

Note: docs on debugging Python can be found here: https://code.visualstudio.com/docs/python/debugging.

There is a property in python-type launch configs called "purpose", where you can put "debug-in-terminal" in the array value, which seems to make it so the launch config will apply when use the "debug in terminal" mode of the play button at the top-right of the screen.

For commandline arguments, use the python.terminal.launchArgs setting. As for PYTHONPATH, that's an environment variable, so you'll have to modify each of terminal.integrated.env.windows, terminal.integrated.env.osx, terminal.integrated.env.linux in your settings.json file (you can do it in the workspace or user settings.json file). Ex.

&quot;terminal.integrated.env.linux&quot;: {
    &quot;FOO&quot;: &quot;foo&quot;,
    &quot;BAR&quot;: &quot;hello&quot;,
    &quot;BAZ&quot;: &quot;baz&quot;,
},
// repeat for OSX and Windows (unfortunately, it&#39;s repetitive)

Note also that since the Python: Run Python File in Terminal feature attempts to reuse the terminal, and that you can't (as far as I know) change a process' environment variables after it has been created, you'll need to close that terminal to get changes you make to the terminal.integrated.env.* to get "picked up" (from the terminal being recreated, and the shell process along with it).

Note: There's a related issue ticket at: Allow "Run Python File in Terminal" button to be configurable #11812.

huangapple
  • 本文由 发表于 2023年2月24日 00:08:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定