10

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 VSCode, 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.

QUESTION: how could I debug this project in VSCode ?

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

Thank you.

Serban
  • 592
  • 1
  • 4
  • 24

3 Answers3

11

For Visual Studio Code, you could try this:

  • add an __init__.py file in the sub-directory my_project
  • in the .vscode directory, add a lauch.json file with the following content:
{
    "version": "0.1.0",
    "configurations": [
        {
            "name": "my-project",
            "type": "python",
            "request": "launch",
            "cwd": "${workspaceFolder}",
            "module": "my_project",
            "args": []
        }
    ]
}

Here, cwd points to your workspace folder, which should be the parent directory of my-project. You should then be able to run successfully the Run and Debug module of Visual Studio Code.

As for Poetry, try modifying your pyproject.toml like this (there seems to be a typo, hyphen vs underscore):

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

And make sure to set the parent directory of my-project as your current working directory when you run poetry run my-project.

See this post for additional guidance.

Laurent
  • 12,287
  • 7
  • 21
  • 37
  • 1
    Should `__init__.py` file be empty ? – Cirelli94 Nov 12 '21 at 09:18
  • Hi, I don't think that the content of the `__init__.py` file matters in that case. – Laurent Nov 13 '21 at 07:03
  • 2
    An empty `__init__.py` file tells python that the folder it is in is a package. This gets your import references working. You could also put python code into `__init__.py`, if needed. That code gets executed if someone imports from the package (i.e. named after the parent folder of the `__init__.py`. These files are one way of managing what exactly gets imported when you import a package. Not typically needed. – LightCC Mar 17 '22 at 04:30
  • 1
    This approach results for me in `No module named my_project.__main__; 'my_project' is a package and cannot be directly executed. – MaKaNu Jun 15 '23 at 10:14
0

In the project directory, create the file .vscode/launch.json with the following structure:

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: <https://go.microsoft.com/fwlink/?linkid=830387>
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Your Project Name",
      "type": "python",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "module": "poetry",
      "python": "${workspaceFolder}/.venv/bin/python",
      "args": [
        "run",
        "python",
        "-m",
        "uvicorn",
        "your_app_project:app",
        "--host",
        "localhost",
        "--port",
        "8000",
        "--debug",
        "--reload"
      ],
      "justMyCode": true,
      "stopOnEntry": false,
      "console": "integratedTerminal",
      "env": {
        "SIMPLE_SETTINGS": "your_env_value"
      }
    }
  ]
}

Adjust the name, the args, and the env to fit your project. If you are using pip, the value of the module field should be pytest.

It is also possible to add more configurations and change them during development.

0

The previous answers didn't work for me.

Here is the configuration of the launch.json file I needed to use to get the python debugger running on Visual Studio Code in a Poetry project.

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "project debug", // Anything you want
        "type": "python",
        "request": "launch",
        "cwd": "C:\\...\\project", // full path to project folder
        "python": "C:\\...\\python.exe", // full path to python.exe (run  poetry env info --path) in terminal to get virtual env folder
        "program": "C:\\...\\file.py", // full path to file to execute
        "console": "integratedTerminal",
        "redirectOutput": true,
        "justMyCode": false,
        "stopOnEntry": false,
      }
    ]
  }

And here's the structure I use(d) for Poetry projects

C:.
│   poetry.lock
│   pyproject.toml
│
├───.vscode
│       launch.json
│
├───decon
│   │   file1.py
│   │   file2.py
│   │   ...
│   │   __init__.py
jlaufer
  • 89
  • 8