Home >Backend Development >Python Tutorial >How to Set the Working Directory for Python Debugging in VS Code?

How to Set the Working Directory for Python Debugging in VS Code?

Barbara Streisand
Barbara StreisandOriginal
2024-10-18 15:07:30235browse

How to Set the Working Directory for Python Debugging in VS Code?

Setting Working Directory for Debugging Python in VS Code

If you need to specify the working directory when running a Python script with the debugger in VS Code, here's how to do it:

Launch.JSON Configuration

In the launch.json configuration file, add the following line to set the working directory dynamically to the directory where the currently open Python file is located:

"cwd": "${fileDirname}"

Be sure to use the correct case for the variable fileDirname.

Purpose Option

Depending on your setup, you may also need to add the following option:

"purpose": ["debug-in-terminal"]

This option is necessary when using the play button on the toolbar to debug.

Sample launch.json

If you're using the "Python: Current File (Integrated Terminal)" option, your launch.json file could look something like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": "${fileDirname}",
            "purpose": ["debug-in-terminal"]
        }
    ]
}

Note:

The launch.json file is usually located in your project directory. If you don't have one, create it by selecting the "Configure gear icon on the Debug view top bar" or by clicking the "Debug button on the navigation panel" and selecting "Create a launch.json file".

The above is the detailed content of How to Set the Working Directory for Python Debugging in VS Code?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn