Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: How to debug your main script from VS Code #9708

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mmartinortiz
Copy link

Resolves: #7592

After having some difficulties to debug the main script of my program while using VS Code, I found this answer that helped me to solve my issue. I thought that would be worth for others to have this piece of information within Poetry's documentation.

@radoering
Copy link
Member

I do not think we should recommend adding poetry to the dev dependencies of the project because this can lead to unexpected behavior. I am not a VS Code user, but I assume you can just configure the correct interpreter (the one returned by poetry env info) and run your entry point directly with this interpreter to debug it.

@mmartinortiz
Copy link
Author

I am not a VS Code user, but I assume you can just configure the correct interpreter (the one returned by poetry env info) and run your entry point directly with this interpreter to debug it.

This is one of the multiple things I tried. Since poetry does not install the package being developed into the virtual environment, the interpreter cannot resolve the imports made from your own module. (At least, that is how understood it while I tried to make it work)

I found some threads in StackOverflow (this one is just an example) about how to get VS Code debug working for a project managed with poetry and I never found a working solution, except for this one, that install poetry as development dependency.

I can understand that it is not neither an elegant or robust solution and I'm very happy to document a more elegant method :-)

@radoering
Copy link
Member

Since poetry does not install the package being developed into the virtual environment,

Poetry installs the package into the virtual environment in editable mode (except for when you set package-mode = false). You should find a *.pth file named after your project in the site packages of the virtual environment after running poetry install.

@mmartinortiz
Copy link
Author

The documentation states that package mode is the default behavior. This is true for my project; however, unless I install poetry as a development dependency, I encounter a 'Module not found' error, and I can't get debugging to work in VS Code.

Clearly, my assumption about the reason for the module not being found was incorrect. What could be then the reason for the debugger to not find the error?

@TheSven73
Copy link
Contributor

vscode debugging works for me without having to install poetry as a development dependency.

First, make sure vscode is using the venv which poetry has created. Run poetry env info and copy the location of the executable. Then, in vscode, run "Python: Select Interpreter" -> "Enter Interpreter path" -> paste the executable location. The bottom right of the vscode window should now show something like this
Screenshot 2024-09-25 at 11 13 14 PM

My main function is located in src/hypermodern_python/console.py:

import click

@click.command()
def main():
    click.echo('Hello, world!')


if __name__ == "__main__":
    main()

Next, create a launch.json which specifies the module which contains main:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "My script",
            "type": "debugpy",
            "request": "launch",
            "cwd": "${workspaceFolder}",
            "module": "hypermodern_python.console",
            "justMyCode": false,
        }
    ]
}

Place a breakpoint, run "Debug: Start Debugging" in vscode, and the breakpoint should be hit.
Screenshot 2024-09-25 at 11 12 00 PM

@mmartinortiz
Copy link
Author

Thanks @TheSven73 , with your launch.json file I managed to debug my main script. I'm not sure how it different to other attempts, maybe it is just combination of justMyCode and module

I've updated my pull request with your instructions.

@TheSven73
Copy link
Contributor

TheSven73 commented Sep 27, 2024

I'm not sure how it different to other attempts

Not sure either, hard to tell without a reproducible description of your attempt(s) (config+code).

@TheSven73 TheSven73 mentioned this pull request Sep 27, 2024
4 tasks
@Secrus
Copy link
Member

Secrus commented Oct 1, 2024

Drive-by question: if we were to include that in docs, what about other editors? Why should we have a special treatment for VS Code?

@mmartinortiz
Copy link
Author

Drive-by question: if we were to include that in docs, what about other editors? Why should we have a special treatment for VS Code?

From my point of view, this is not giving a special treatment to VS Code.

When I found myself in need to debug a project managed with Poetry, setting it up in VS Code was not as straight forward as it is for projects not managed with Poetry. After searching for "how to do it" I found more people straggled with this, proposing different alternatives.

After I succeeded, and given that more people struggled with this, I thought would be worth to share the "how to" together with poetry's documentation.

@TheSven73
Copy link
Contributor

TheSven73 commented Oct 1, 2024

I'm not convinced we have a clear definition yet of the problem we're trying to solve.

I just tried the official Microsoft tutorial to debug Flask and it works like a charm with Poetry. The only difference is that the developer must point vscode to the venv created by poetry, instead of hand-rolling their own using python -m venv and pip.

Can someone in the linked issue(s) can provide a clear and reproducible problem statement? Other than: "must tell the user to point vscode to the poetry venv", because that's true for other editors as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to debug with Debugpy
4 participants