简体   繁体   English

如何在 VS Code 中使用 Python 从当前目录调试文件?

[英]How to debug a file from current directory with Python in VS Code?

I can see that this question has been asked multiple times already however, none of the solutions seems to be working anymore, forcing me to ask for the latest fix.我可以看到这个问题已经被问过多次,但是,似乎没有任何解决方案有效,迫使我寻求最新的修复。

Problem问题

I have created a workspace in VS code with the following directory structure,我在 VS 代码中创建了一个具有以下目录结构的工作区,

VS代码目录结构

  • work__assignments (this is my main workspace folder in vs code) work__assignments(这是我在 vs 代码中的主要工作区文件夹)
    • assignment__1作业__1
      • data数据
      • modules模块
      • tests测试
    • assignment__2作业__2
      • data数据
      • modules模块
      • tests测试

"work__assignments" is my main workspace folder in the VS code. “work__assignments”是我在 VS 代码中的主要工作区文件夹。 From this main workspace, I go to an assignment folder (eg work__assignments > assignment__2 > modules ) and work on the respective code.从这个主工作区,我 go 到作业文件夹(例如work__assignments > assignment__2 > modules )并处理相应的代码。 However, when I try to debug the code in " work__assignments > assignment__2 > modules " the debugger loads from the main workspace folder (ie work__assignments), then it fails because it can't find other modules in the modules folder " work__assignments > assignment__2 > modules ".但是,当我尝试调试“ work__assignments > assignment__2 > modules ”中的代码时,调试器从主工作区文件夹(即 work__assignments)加载,然后失败,因为它无法在模块文件夹“ work__assignments > assignment__2 > ”中找到其他模块模块”。

I have tried the following methods so far,到目前为止,我已经尝试了以下方法,

  1. Updating the launch.json file and adding the line "cwd": "${fileDirname}" ,更新 launch.json 文件并添加行"cwd": "${fileDirname}"
{
    "name": "Python: Current File",
    "type": "python",
    "request": "launch",
    "program": "${file}",
    "console": "integratedTerminal",
    "cwd": "${fileDirname}"
}
  1. Updating the launch.json file and adding the line "cwd": "" ,更新 launch.json 文件并添加行"cwd": ""
{
    "name": "Python: Current File",
    "type": "python",
    "request": "launch",
    "program": "${file}",
    "console": "integratedTerminal",
    "cwd": ""
}
  1. Updating the launch.json file and adding the following lines,更新 launch.json 文件并添加以下行,
{
    "name": "Python: Current File",
    "type": "python",
    "request": "launch",
    "program": "${file}",
    "console": "integratedTerminal",
    "cwd": "${workspaceFolder}",
    "env": {
        "PYTHONPATH": "${cwd}"
    }
}
  1. Updating the preferences更新首选项

    文件 > 首选项 > 设置 > Python > 在文件目录中执行

However, NONE OF THESE APPROACHES HAVE WORKED.然而,这些方法都没有奏效。 Every time I debug the code, it launches into the workspace folder and not in the code folder.每次我调试代码时,它都会启动到工作区文件夹而不是代码文件夹中。 What am I doing wrong?我究竟做错了什么?

Note: I am running the debugger using the "Debug Python File" button at the top right corner as shown in the picture below,注意:我正在使用右上角的“调试 Python 文件”按钮运行调试器,如下图所示, 在此处输入图像描述

Configure one of the following cwd in launch.json :launch.json中配置以下cwd之一:

    "cwd": "./assignment__2"
    "cwd": "${fileDirname}"

It should be noted that you need to debug the program from the Run and Debug panel to execute the configuration in launch.json .需要注意的是,您需要从运行和调试面板调试程序才能执行launch.json中的配置。

在此处输入图像描述

A simple example一个简单的例子

Directory Structure and code目录结构和代码在此处输入图像描述

launch.json启动.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            // "cwd": "./hello"
            "cwd": "${fileDirname}"
        }
    ]
}

Debug results调试结果在此处输入图像描述

Invalid configuration in launch.json if debug is selected from play button如果从播放按钮选择调试,启动中的配置无效launch.json 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM