Home  >  Article  >  Backend Development  >  How to Set the Working Directory for Python Debugging in Visual Studio Code?

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

Patricia Arquette
Patricia ArquetteOriginal
2024-10-18 15:10:03898browse

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

How to Set the Working Directory for Debugging a Python Program with VS Code's Debugger?

When debugging a Python program with Visual Studio Code (VS Code), specifying the working directory is crucial to ensure that your script runs correctly.

To set the working directory in your launch configuration file (launch.json), follow these steps:

  1. Open your launch.json file:

    • In VS Code, navigate to the Run view by clicking the Run icon on the sidebar.
    • Select the Configure (gear) icon on the top toolbar.
    • Click on Add Configuration... and choose Python:
  2. Set the "cwd" variable:

    • In the launch configuration, locate the "configurations" section.
    • Within the first configuration, insert the following line:
    <code class="json">"cwd": "${fileDirname}"</code>
    • This specifies that the working directory will be set to the directory of the currently open Python file.
  3. Consider the "purpose" option (optional):

    • If you plan to use the "Run and Debug" icon in the sidebar or the Run Python File in Terminal option, add the following line:
    <code class="json">"purpose": ["debug-in-terminal"]</code>
  4. Save your launch.json file:

    • Ensure that you save your launch.json file in the same directory as your Python script.

Example launch.json configuration:

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

Note: The launch.json file controls the debug settings for your project. If you do not have one, create it by clicking the Configure gear icon in the Debug view.

The above is the detailed content of How to Set the Working Directory for Python Debugging in Visual Studio 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