Python project created using Poetry: how do I debug it in Visual Studio Code?
Question:
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.
Answers:
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.
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.
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.
For Visual Studio Code, you could try this:
- add an
__init__.py
file in the sub-directorymy_project
- in the
.vscode
directory, add alauch.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.
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.