运行 main() 项目文件的快捷方式

Shortcut to run main() project file

我正在用 VS Code 编写 Python,但似乎找不到解决方案。

我想要一个始终运行包含 "main()"

的主项目文件的快捷键

到目前为止我发现的所有内容都只运行当前的编辑器文件,而且每次都必须切换回该文件,它很快就变旧了。

创建以下启动配置(将 MyMain.py 更改为您的主文件)

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Main File",
      "type": "python",
      "request": "launch",
      "program": "${workspaceFolder}/MyMain.py",
      "console": "integratedTerminal",
      "cwd": "${workspaceFolder}"
    }
  ]
}

如果您只想在终端中对 运行 进行键绑定,请尝试:

{
    "key": "alt+x",                                      // whatever keybinding you choose
    "command": "workbench.action.terminal.sendSequence",
     //  "args": {"text": "python ${file}\u000d"}
     "args": {"text": "python MyFileName\u000d"}
}

${file} 将始终 运行 当前文件,您不想要它,因此请将 ${file} 替换为您确实想要 运行.[=15 的文件名=]

\u000d 是一个 return,所以它会立即 运行。

send text to terminal with a keybinding. In addition to a hard-coded reference to some filename, you can also use vscode's variables