Search code examples
pythonvisual-studio-codecommand-line-interfacepython-poetry

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


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.


Solution

  • 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:

     {
        "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 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.