Modify the root directory of vscode code debugging

By default, the root directory for code debugging, that is, the root directory of the folder we opened. As shown in the figure, the folder "1" is the root directory where the default code runs. folder structure
Assuming that the code folder is the "222" folder, if we open a file with a relative path in our code, such as config.json, then the vscode debugger will search with the "1" root directory. At this point, an error will be reported. The following code:

# test.py
import json
with open("config.json", "r") as json_data_file:
    cfg = json.load(json_data_file)

Of course we can modify the code, but this will break the original code and destroy portability.
We can solve this problem by modifying the default working directory.
The specific method is to open launch.json , modify the **"cwd"** parameter, and change it to the root directory of our code. As shown in the following code. By default, there is no "cwd" parameter, we need to add it manually.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "cwd": "${workspaceFolder}/222"
        }
    ]
}

Note: The slashes here are /, not \.
"cwd": "${workspaceFolder}""1" is the root directory of the opened folder, which is the default value of the original vscode.
After modifying the cwd parameter, the code can run normally.

Guess you like

Origin blog.csdn.net/m0_59019651/article/details/123889523