如何使用VSCode调试Python命令行二进制文件(特指Poetry)。

huangapple go评论55阅读模式
英文:

How to debug python cli binary (specifically poetry) with vscode

问题

I can provide a translation for the text you provided:

我在使用Windows、Cygwin、WSL等工具执行某些命令(特别是 "poetry add source...")时遇到了各种问题——证书错误、'is a directory' 错误,还有一个我记不清的问题。如果这只是Python代码中的简单导入,我可以轻松地进入模块代码并了解发生了什么。我也不介意将我发现的任何问题报告给开发人员。然而,我不知道如何在使用CLI二进制Python命令(在这种情况下是poetry,但也可能是pip或其他命令)时进行此类调试。我想在VSCode中进行此调试,但我想在PyCharm中设置类似的调试方式也可以。

我想 python -m poetry 可能是其中的一部分。我看到VSCode支持 module 运行配置,但我不知道一旦运行后如何进入它。我在poetry文档中没有看到任何与开发设置相关的信息。我想我可以在一个脚本中修改一些 main() 入口点,将参数发送给poetry模块(而不是CLI二进制),然后以这种方式进入,但我不太确定在哪里进入,或者这是否是最佳方法。我应该如何在将poetry用作CLI二进制时进行调试,以便可以设置断点并逐步调试以找出发生所有错误的原因。

英文:

I'm running into all sorts of problems with my setup running certain commands (using windows, cygwin, wsl) with poetry (in particular "poetry add source...")--certificate errors, 'is a directory' errors, and one other I cant recall. If this was a simple import in python code I'd be able to just step into module code and get an idea of what is going on. And wouldn't mind passing on any issues I find to the devs. However, I don't know how to do this type of debugging with a cli binary python command (poetry in this case, but could be pip or something else). (I'd prefer to do this in vscode but I suppose something in pycharm might let me set something similar in vscode).

I figure python -m poetry might be part of it. I see vscode supports module run configuration, but I don't see how I get an entrypoint into it once I run. I don't see any mention of development setup in the poetry documentation. I suppose I may be able to hack some main() entry point in a script which sends arguments to poetry module (not cli binary) and enter that way, but not quite sure where to enter or if that's even the best way. How can I debug poetry when I'm using it as a cli binary so I can set breakpoints and step back trying to find out what's going on with all my errors.

答案1

得分: 1

I can help you translate the provided text:

"Found out how (this is in cygwin, but should be similar in other terminals). You'll need to make sure you have your binaries source (although you can probably attach to a running process, with a short-running CLI command that takes many arguments it doesn't look to be easy.)
In VSCode use the following launch.conf:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "poetry",
            "justMyCode": false,
            "cwd": "C:\\Users\\<username>\\python3.8.10\\Lib\\site-packages",
            "args": [<arguments for the command you want to debug>],
        },
    ]
}

Then run in the case of poetry run __main__.py as the entry point (or the equivalent main() in your CLI binary). It should run your command, and you can debug the issue. Of course, with a CLI, you're probably not getting thrown exceptions; you may have external library warnings and no stack trace, but if you search code and set breakpoints, you can probably find where the warnings and issues are appearing."

英文:

Found out how (this is in cygwin, but shoudl be similar in other terminals). You'll need to make sure you have your binaries source (although you can probably attach to a running process, with a short-running CLI command that takes many arguments it doesn't look to be easy.)
In VSCode use the following launch.conf:

 {
    &quot;version&quot;: &quot;0.2.0&quot;,
    &quot;configurations&quot;: [
        {
            &quot;name&quot;: &quot;Python: Module&quot;,
            &quot;type&quot;: &quot;python&quot;,
            &quot;request&quot;: &quot;launch&quot;,
            &quot;module&quot;: &quot;poetry&quot;,
            &quot;justMyCode&quot;: false,
            &quot;cwd&quot;: &quot;C:\\Users\\&lt;username&gt;\\python3.8.10\\Lib\\site-packages&quot;,
            &quot;args&quot;: [ &lt;arguments for the command you want to debug&gt;
            ],
        },
    ]
}

Then run in the case of poetry run __main__.py as the entry point (or the equivalent main() in your cli binary). It should run your command and you can debug issue. Of course with a CLI you're probably not getting thrown exceptions, you may have external library warnings and no stacktrace, but if you search code and set breakpoints you can probably find where the warnings and issues are appearing.

huangapple
  • 本文由 发表于 2023年5月18日 04:04:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定