python-poetry / poetry

Python packaging and dependency management made easy
https://python-poetry.org
MIT License
31.88k stars 2.28k forks source link

Unable to debug with Debugpy #7592

Open delucca opened 1 year ago

delucca commented 1 year ago

Issue

Following up on this issue, for some reason nvim-dap is not being able to attach a debugger if I'm running an application using Poetry

If I run using Python directly it works, but using Poetry it automatically closes. If I run from within a Poetry shell, it bootstraps the debugger, but whenever I set breakpoints debugpy throws an error

You can see the details on the original issue.

To replicate the issue you just need to do the following:

  1. Create the basic setup without Poetry:

    python -m venv venv
    venv/bin/python -m pip install Flask
  2. Create a vscode/launch.json:

    {
    "version": "0.2.0",
    "configurations": [
       {
           "type": "python",
           "request": "launch",
           "name": "Flask",
           "module": "flask",
           "args": ["--app", "foo", "run"],
           "console": "integratedTerminal"
       }
    ]
    }
  3. Create foo.py:

    
    from flask import Flask

app = Flask(name)

@app.route("/") def hello(): return "

Hello World

"



After this, run in your IDE, attach a debugger to it and test. You'll see that it works.

Now, do the same, but on the setup step do the setup using Poetry. After this, tried running the debugger with the Poetry setup. It would either not work (if you try using it with `poetry run`) or it will not be able to set any breakpoints (if you try using `poetry shell`)
dimbleby commented 1 year ago

You don't have to poetry run everything! Just activate the virtual environment yourself and run things directly.

delucca commented 1 year ago

@dimbleby I think you're mixing this issue with the other. For this issue I'm not using poetry run (I've never said I was in this specific issue)

For this issue, even in a Poetry shell, the issue is the same. If I install the application with Poetry, Flask debugger doesnt work for some reason (with Nvim). If I install the same application without using Poetry (in a common venv) it works

To be 100% clear, in the Poetry shell the debugger is able to bootstrap, but setting breakpoints breaks the application for some reason.

dimbleby commented 1 year ago

all you have given us here is a link to another issue, in which the configuration shows that you are calling poetry run

    program = "${env:PYTHON_VENV_PATH}/bin/poetry",
    args = {
        "run",
        "flask",
        "--no-debug",
        "run",
    },

if you're now saying that's not what you're doing then I don't even

delucca commented 1 year ago

@dimbleby as I've mentioned, the same thing happens if you open a shell instead of using poetry run (as I've mentioned in the issue)

dimbleby commented 1 year ago

do you mean poetry shell? I wouldn't do that either: just activate the virtual environment in the regular way and run things directly.

This report is too involved and too confused: if you can reduce it to something that reproduces without having to install neovim and dap and flask then you'll have a better chance of someone taking an interest in it.

delucca commented 1 year ago

@dimbleby you can use the same example from the original issue (on the last comment from the nvim-dap-python author)

I can create a github repo if you want, but essentially you just need to launch any simple Python application. Unfortunately I don't see any possible alternative of exploring this issue without having to install nvim and nvim-dap, since the issue is specifically a problem with debugging in that specific tool 😄

dimbleby commented 1 year ago

you've linked to a comment which says that "it works for me.". Plainly that is not a way to reproduce your problem.

I'm gonna duck out, this is going nowhere.

Maybe I'm being grumpy but I think this is good advice: you'll have a better chance of getting help from people if you make it as easy as possible for them. Links to complicated issues, with not very much information, and contradictory comments... are not that.

delucca commented 1 year ago

@dimbleby if you really wants to help and understand the problem, take some time to read the original issue.

It isn't a "it works for me" issue. It contains a minimal reproduction. You just need to duplicate that code on your machine and run it, with and without using Poetry

As I've mentioned, if you want I can create a repository to simplify, but it is just a single file, I don't see any point of creating a repo for that

vantaboard commented 10 months ago

I found a really nice way of doing this after banging my head on my desk over it.

.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug: start",
            "type": "debugpy",
            "request": "launch",
            "module": "poetry",
            "args": [
                "run",
                "start"
            ],
            "justMyCode": false
        }
    ]
}

poetry.toml

[virtualenvs]
in-project = true

And then the final touch is to run

poetry add --group dev poetry

which bootstraps poetry and enables its use with debugpy without any of the extra headache of switching your environment, etc.

TheSven73 commented 2 months ago

Do the instructions in https://github.com/python-poetry/poetry/pull/9708#issuecomment-2375769242 fix this issue?

If not, please provide clear instructions on how to reproduce, with sample code, configs, etc.