首頁 > 後端開發 > Python教學 > 如何在 VS Code 中設定工作目錄以實現高效 Python 偵錯?

如何在 VS Code 中設定工作目錄以實現高效 Python 偵錯?

Barbara Streisand
發布: 2024-10-18 15:08:30
原創
580 人瀏覽過

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

Setting the Working Directory for Python Debugging in VS Code

For seamless debugging in VS Code, specifying the working directory is crucial. Here's how to achieve it:

Dynamic Working Directory via launch.json

Utilize the "cwd" option in launch.json to set the working directory to the location of the open Python file. This dynamic approach ensures that the correct directory is always used:

<code class="json">"cwd": "${fileDirname}"</code>
登入後複製

Note: Ensure capitalization and correct spelling of "fileDirname."

Additional Configuration (Optional)

Depending on your debugging method, you may need to include the "purpose" option:

<code class="json">"purpose": ["debug-in-terminal"]</code>
登入後複製

This option is necessary if using the play button or "Run and Debug" sidebar option.

Example Launch.json Configuration

For debugging with the Python: Current File (Integrated Terminal) option, your launch.json might resemble:

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

Creating a launch.json File

If you don't have a launch.json file, follow these steps:

  • Open the project folder in VS Code.
  • Click the "Configure" gear icon in the Debug view top bar.
  • Click "Create a launch.json file."
  • Select Python as the debugging language.

Alternative Method

  • Click the Debug button on the navigation panel.
  • Select "Create..." when prompted to create a launch.json file.
  • Choose Python as the debugging language.

以上是如何在 VS Code 中設定工作目錄以實現高效 Python 偵錯?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板