Home > Backend Development > Python Tutorial > 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
Release: 2024-10-18 15:10:03
Original
1048 people have browsed it

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>
    Copy after login
    • 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>
    Copy after login
  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>
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template