From b4e73873173021c354cfc0af77728e071f8f1305 Mon Sep 17 00:00:00 2001 From: manolo Date: Wed, 25 Sep 2024 15:12:08 +0200 Subject: [PATCH 1/2] docs: How to debug your main script from VS Code --- docs/faq.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/docs/faq.md b/docs/faq.md index c35a92326a6..77a10baade6 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -271,3 +271,50 @@ The two key options we are using here are `--no-root` (skips installing the proj Poetry's default HTTP request timeout is 15 seconds, the same as `pip`. Similar to `PIP_REQUESTS_TIMEOUT`, the **experimental** environment variable `POETRY_REQUESTS_TIMEOUT` can be set to alter this value. + +### How to debug the main script of your project using Poetry, VS Code and `launch.json` + +To debug a project that is managed by `poetry` and has a main script, you need to install `poetry` as a development dependency and use the `poetry run` command to execute the script via the `launch.json` file, and configure `poetry` to create the virtual environment in the project directory. + +Make sure that `poetry` creates the virtual environment in your project directory. If that is not your case, you will have to recreate your virtual environment after the configuration options is changed. + +```bash +poetry config virtualenvs.in-project true +``` + +Given that your `pyproject.toml` has the following entry: + +```toml +[tool.poetry.scripts] +my_program = "my_package.my_program:main" +``` + +Add `poetry` as a development dependency: + +```bash +poetry add --group dev poetry +``` + +Then, create a `launch.json` file (if it does not exists already) in the `.vscode` directory of your project with the following content: + +```json +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Debug my_program", + "type": "python", + "request": "launch", + "console": "integratedTerminal", + "module": "poetry", + "args": [ + "run", + "my_program", + // Add other arguments for your program here if the are required + ] + } + ] +} +``` + +Now you can start the debugger in VS Code and it will run starting from the main script of your project. From adf0771d5ac2ed3c733232ad6fb41bc41a2ed3f5 Mon Sep 17 00:00:00 2001 From: manolo Date: Fri, 27 Sep 2024 11:43:14 +0200 Subject: [PATCH 2/2] docs: How to debug in VS Code without poetry as dev dependency --- docs/faq.md | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/docs/faq.md b/docs/faq.md index 77a10baade6..daf12650837 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -274,28 +274,19 @@ can be set to alter this value. ### How to debug the main script of your project using Poetry, VS Code and `launch.json` -To debug a project that is managed by `poetry` and has a main script, you need to install `poetry` as a development dependency and use the `poetry run` command to execute the script via the `launch.json` file, and configure `poetry` to create the virtual environment in the project directory. +Make sure that VS Code is using the `venv` created by `poetry` as the Python interpreter. You can obtain this information running `poetry env info` in the terminal. -Make sure that `poetry` creates the virtual environment in your project directory. If that is not your case, you will have to recreate your virtual environment after the configuration options is changed. +Given that your entry point is located at `src/my_package/my_program.py` and it looks like: -```bash -poetry config virtualenvs.in-project true -``` - -Given that your `pyproject.toml` has the following entry: - -```toml -[tool.poetry.scripts] -my_program = "my_package.my_program:main" -``` +```python +def main(): + print("Hello, World!") -Add `poetry` as a development dependency: - -```bash -poetry add --group dev poetry +if __name__ == "__main__": + main() ``` -Then, create a `launch.json` file (if it does not exists already) in the `.vscode` directory of your project with the following content: +Create a `launch.json` file in the `.vscode` directory of your project with the following content: ```json { @@ -303,15 +294,14 @@ Then, create a `launch.json` file (if it does not exists already) in the `.vscod "configurations": [ { "name": "Python: Debug my_program", - "type": "python", + "type": "debugpy", "request": "launch", - "console": "integratedTerminal", - "module": "poetry", - "args": [ - "run", - "my_program", - // Add other arguments for your program here if the are required - ] + "cwd": "${workspaceFolder}", + // Remember to adjust this setting + "module": "my_package.my_program", + "justMyCode": false, + // You can include other arguments for your program here if they are required + "args": [] } ] }