[Python] Python project with Poetry: how to debug it in Visual Studio Code?

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Python project which I created according to basic Poetry instructions.

    The project folder is something like this:

    my-project
    +----my_project
    | +-- my_project.py
    | +-- File1.py
    | +-- File2.py
    |
    +----pyproject.toml


    Example of how I import stuff from one file to another: in my_project.py I have the code

    from . import File1, File2


    If I want to debug this from VS Code, if I try F5 in the my_project.py, I get the error:

    Exception has occurred: ImportError
    attempted relative import with no known parent package


    However, if I don't express the imports like above, I can't run it using the poetry command.

    In the pyproject.toml file, I have this:

    [tool.poetry.scripts]
    my-project = "my_project.my_project:run"


    run is the entry-point method in the my_project.py file.

    To run the project from command prompt, I go to the project folder (where the package folder is) and I type poetry run my-project

    Again, up to this point, everything according to the Poetry documentation.

    How could I debug this project in VS Code?

    I know I need to create a launch.json file, but I don't know how the configuration should look.

    Continue reading...

Compartilhe esta Página