Comment définir le répertoire de travail pour le débogage Python dans Visual Studio Code ?

Patricia Arquette
Libérer: 2024-10-18 15:10:03
original
1010 Les gens l'ont consulté

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>
    Copier après la connexion
    • 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>
    Copier après la connexion
  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>
Copier après la connexion

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.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:php
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal