如何使VSCode始终运行main.py

时间:2019-12-18 15:16:39

标签: python visual-studio-code

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

2 个答案:

答案 0 :(得分:1)

您可以使用密钥launch.json的以下设置来修改program。您可能需要将program指向要执行的文件。在以下情况下,main.py仅存在于我的工作区文件夹中。您可以根据需要进行修改。

{
    // 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"
        }
    ]
}

答案 1 :(得分:0)

您需要将'launch.json'放在工作空间内的'.vscode'文件夹下。然后Run> Run Without Debugging(Windows CTRL+F5上的快捷键)

enter image description here

launch.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"
        }
    ]
}