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.
For Visual Studio Code, you could try this:
__init__.py
file in the sub-directory my_project
.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.