"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > 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?

Published on 2024-11-08
Browse:154

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:
    "cwd": "${fileDirname}"
    • 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:
    "purpose": ["debug-in-terminal"]
  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:

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

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.

Release Statement This article is reprinted at: 1729235357 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3