简体   繁体   English

如何在 Visual Studio Code 的 launch.json 中调试 Python 模块

[英]How to debug a Python module in Visual Studio Code's launch.json

My question may seem simple but, I have a module that I launch in a terminal like this:我的问题可能看起来很简单,但是,我有一个在终端中启动的模块,如下所示:

python -m my_module.my_file

How do I debug this in Visual Studio Code?如何在 Visual Studio Code 中调试它?

I have this in my launch.json ( documentation )我的launch.json文档)中有这个

"type": "python",
"request": "launch",
"pythonPath": "D:\\ProgramData\\Anaconda3\\envs\\simulec\\python.exe",
"args": [
   "-m",
   "my_module.my_file",
]

If I don't set the program option or if I set it to "" I get "File does not exist" Error.如果我没有设置program选项,或者如果我将它设置为"" ,我会收到“文件不存在”错误。

How can I fix this?我怎样才能解决这个问题?

Actually, there is a very simple option to do this I found by accident while trying to edit the launch.json file.实际上,我在尝试编辑launch.json文件时偶然发现了一个非常简单的选项。

"type": "python",
"request": "launch",
"pythonPath": "D:\\ProgramData\\Anaconda3\\envs\\simulec\\python.exe",
"module": "my_module.my_file",

Simply specify the module in the module key "module": "my_module.my_file"只需在模块键"module": "my_module.my_file"指定模块"module": "my_module.my_file"

The -m is not useful any more. -m不再有用。

In a terminal like this: python -m xyz abc.z3在这样的终端中: python -m xyz abc.z3

(Please make sure you are opening "the root folder of your project"). (请确保您正在打开“项目的根文件夹”)。

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "module",
            "type": "python",
            "request": "launch",
            "module": "xyz",
            "args": [
                "abc.z3"
            ]
        }
    ],
}

The answer from @dzada was a bit confusing for me so I tried to rephrase it and add more clarification. @dzada 的回答让我有点困惑,所以我尝试重新措辞并添加更多说明。

To debug a module in file called script_file.py that exists in a package called packagex with structure like the following:要调试名为script_file.py文件中的模块,该文件存在于名为packagex的包中,其结构如下:

Project_Folder
   packagex
      script_file.py

Your configuration in the launch.json file should looks like the following launch.json文件中的配置应如下所示

    "configurations": [
    {
        "name": "Python: any name like script_file",
        "type": "python",
        "request": "launch",
        "module": "packagex.script_file"
    }
]

To add slightly to dzada's answer , which helped me a lot, a Visual Studio Code variable can be used to make this general for debugging any file that is in your module.为了稍微增加dzada 的回答,这对我有很大帮助,可以使用 Visual Studio Code 变量使其通用,以调试模块中的任何文件。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "my_module.${fileBasenameNoExtension}"
        }
    ]
}

Which is probably what you want to do.这可能是您想要做的。

After some searching, I found that the config can be generalized for any module by the use of the Command Variable extension .经过一些搜索,我发现可以通过使用Command Variable extension为任何模块推广配置。 Assuming that the your workspace folder is the one containing the package, the following configuration would work for any module:假设您的工作区文件夹是包含 package 的文件夹,以下配置适用于任何模块:

{
    "name": "Python: Module",
    "type": "python",
    "request": "launch",
    "justMyCode": true,
    "module": "${command:extension.commandvariable.file.relativeFileDotsNoExtension}",
    "cwd":"${workspaceFolder}"
}
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "module",
            "type": "pythonExperimental",
            "request": "launch",
            "module": "my_package.my_module.${fileBasenameNoExtension}",
        },
    ]
}

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

相关问题 Visual Studio Code Python,在launch.json中确定启动了哪个模块 - Visual Studio Code Python, determine in launch.json which module is started Visual Studio Code launch.json 设置用于调试 Python 模块 - Visual Studio Code launch.json settings for debugging Python modules 在 Visual Studio Code 中为文件“launch.json”设置 Python 路径 - Setting the Python path in Visual Studio Code for file 'launch.json' 如何使 VScode launch.json 用于 Python 模块 - How to make VScode launch.json for a Python module vscode python调试,launch.json中带有参数语法错误 - vscode python debug with arguments syntax error in launch.json 如何通过 Launch.json 传递 arguments 以在 Visual Studio 2019 中进行调试 - How can I pass arguments via Launch.json for debugging in Visual Studio 2019 如何在Visual Code中设置launch.json来调试C - How can I set up launch.json in Visual Code to debbug C 如何通过 launch.json 在 VS 代码中将参数传递给 python 解释器(而不是 python 代码) - How to pass an argument to the python interpreter (not to the python code) in VS code via launch.json 如何在 VS 代码中配置 launch.json 以在 Windows 中接受网络路径(python) - how to configure the launch.json in VS code to accept a network path (python) in windows 如何在 Visual Studio Code 中调试 python 单元测试? - how to debug python unittests in Visual Studio Code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM