如何让VSCode始终运行main.py

ipr*_*tra 7 python visual-studio-code

我正在用 Python 编写我的第一个库,在开发时,我希望 VS Code 中的运行代码按钮始终从根目录中的 main.py 文件开始运行代码。我已向 launch.json 添加了新配置,但我似乎无法将此配置用作默认配置。我怎样才能做到这一点/

Nai*_*san 9

您需要将“ launch.json”放在工作区中的“ ”文件夹下.vscode。然后Run> Run Without Debugging(Windows 上的快捷方式CTRL+F5

在此输入图像描述

启动.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/main.py",
            "console": "integratedTerminal"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)


小智 2

我发现正确的解决方案是将 launch.json 中的“program”更改为:

"program": "main.py",
Run Code Online (Sandbox Code Playgroud)

如果尝试添加 {workspaceFolder} 则会给出 FileNotFoundError。