【问题标题】:How to debug a Python module in Visual Studio Code's launch.json如何在 Visual Studio Code launch.json 中调试 Python 模块
【发布时间】:2018-02-16 12:56:11
【问题描述】:

我的问题可能看起来很简单,但是我有一个模块,我在这样的终端中启动:

python -m my_module.my_file

如何在 Visual Studio Code 中进行调试?

我的launch.json (documentation) 里有这个

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

如果我没有设置 program 选项,或者如果我将其设置为 "",我会收到“文件不存在”错误。

我该如何解决这个问题?

【问题讨论】:

    标签: python visual-studio-code


    【解决方案1】:

    实际上,我在尝试编辑launch.json 文件时偶然发现了一个非常简单的选项。

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

    只需在模块键"module": "my_module.my_file"中指定模块即可

    -m 不再有用了。

    【讨论】:

    • 谢谢!我很难找到这个。仔细阅读文档后,我发现在 python 调试下提到了这个选项:code.visualstudio.com/docs/python/…
    • 感谢它的工作!但是,即使我在launch.json中设置了“cwd”:“${workspaceFolder}”,也找不到relaive导入的包。有什么解决办法吗?
    【解决方案2】:

    在这样的终端中:python -m xyz abc.z3

    (请确保您正在打开“项目的根文件夹”)。

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

    【讨论】:

      【解决方案3】:

      @dzada 的回答让我有点困惑,所以我尝试重新措辞并添加更多说明。

      在名为 script_file.py 的文件中调试模块,该文件存在于名为 packagex 的包中,其结构如下:

      Project_Folder
         packagex
            script_file.py
      

      launch.json 文件中的配置应如下所示

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

      【讨论】:

        【解决方案4】:

        为了对dzada's answer 稍加补充,这对我有很大帮助,Visual Studio Code 变量可用于使其通用,以调试模块中的任何文件。

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

        这可能是您想要做的。

        【讨论】:

          【解决方案5】:
          {
              "version": "0.2.0",
              "configurations": [
                  {
                      "name": "module",
                      "type": "pythonExperimental",
                      "request": "launch",
                      "module": "my_package.my_module.${fileBasenameNoExtension}",
                  },
              ]
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-04-16
            • 1970-01-01
            • 2016-09-04
            • 2018-05-19
            • 2019-05-25
            • 2020-01-13
            • 2016-08-19
            相关资源
            最近更新 更多