VSCode + debugging + poetry #9206
-
Hi, we use poetry and I like to use my poetry scripts as entry points for the debugging in vscode. Therefore, we use following vscode launch.json configurations: {
"configurations": [
{
"name": "poet_####",
"type": "python",
"request": "launch",
"module": "poetry",
"subProcess": true,
"args": [
"run",
"###name###",
],
"console": "integratedTerminal",
"justMyCode": true,
"cwd": "${workspaceFolder}",
"envFile": "",
"env": {},
},
]
} The issue is that the debugging starts BUT exits early (after some seconds). In the middle of the process, everything stops. This is the case for all our code which is run via poetry. If we run the python code manually without the If I run in a shell manually poetry run ###name### it also works flawlessly. In principle the debugging with poetry seems to work. It stops at breakpoints and shows error msgs etc. But as mentioned, shortly after that the whole execution stops and I am out of the debugging and the process itself. Anyone an idea what causes the early exiting? VSCode: 1.65.2
|
Beta Was this translation helpful? Give feedback.
Replies: 14 comments
-
@finswimmer This might be related? |
Beta Was this translation helpful? Give feedback.
-
Same issue here. This is my config: {
"version": "0.2.0",
"configurations": [
{
"name": "Pizzas service",
"type": "python",
"request": "launch",
"cwd": "${workspaceFolder}/backend/pizzas",
"module": "poetry",
"justMyCode": false,
"args": [
"run",
"uvicorn",
"src.main:app",
"--host",
"0.0.0.0",
"--port",
"8000",
"--reload"
]
}
]
} |
Beta Was this translation helpful? Give feedback.
-
I FOUND A SOLUTION 😄 🥳Pre requirementsPoetry creates environments in a cache folder. So, first of all, this needs to be resolved. We need a more consistent and specific environment path. In my case I run: rm -rf ~/.cache/pypoetry/virtualenvs/pizzas-wQgq9NEZ-py3.10 After that, you need to specify a new environment directory: poetry config virtualenvs.in-project true This will create a new Because we created a new environment, we have to install the dependencies again. poetry install ConfigWith the prerequisite fixed, you can use a configuration similar to the following: {
"version": "0.2.0",
"configurations": [
{
"name": "Pizzas service",
"type": "python",
"request": "launch",
"pythonPath": "${workspaceFolder}/backend/pizzas/.venv/bin/python",
"cwd": "${workspaceFolder}/backend/pizzas",
"module": "uvicorn",
"justMyCode": false,
"args": [
"src.main:app",
"--host",
"0.0.0.0",
"--port",
"8000",
"--reload"
]
}
]
} |
Beta Was this translation helpful? Give feedback.
-
@lucasvazq your solution solve my problems, congrats bro!!! |
Beta Was this translation helpful? Give feedback.
-
thank you bro |
Beta Was this translation helpful? Give feedback.
-
I my case, @lucasvazq solution worked with just the |
Beta Was this translation helpful? Give feedback.
-
@lucasvazq, thank you so much! I followed your short "guide" and was able to set up debugging pytest in a poetry project. I just had to tweak the {
"version": "0.2.0",
"configurations": [
{
"name": "Python: run poetry pytest",
"type": "python",
"request": "launch",
"pythonPath": "${workspaceFolder}/.venv/Scripts/python.exe",
"cwd": "${workspaceFolder}",
"module": "pytest",
"args": [
],
"console": "integratedTerminal",
"justMyCode": false,
}
]
} |
Beta Was this translation helpful? Give feedback.
-
You can also use
|
Beta Was this translation helpful? Give feedback.
-
Another way to resolve this is to make your package have a module by creating
# __main__.py
from .project import real_main
# For Click, no arguments and the return value does not matter so do not use sys.exit(real_main())
real_main() Then in {
"configurations": [
{
"args": ["-v"],
"console": "integratedTerminal",
"module": "project",
"name": "project -v",
"request": "launch",
"showReturnValue": true,
"type": "python"
}
],
"version": "0.2.0"
} |
Beta Was this translation helpful? Give feedback.
-
In my current test (as of Sep 25th 2023), below is still valid but we don't need to set this config anymore:
Also we don't need to set the "pythonPath" in the setting anymore (as VSCode does not support that anymore?).
|
Beta Was this translation helpful? Give feedback.
-
What's the solution if we just want to run the .py file directly, but via Poetry? Poetry isn't actually installed in the venv, so I can't call it. Just trying to do whatever the equivalent of |
Beta Was this translation helpful? Give feedback.
-
The only clean way I've found is to use A fairly lazy example would be to use an arbitrary env var such as PYDBG and drop this is your main file: if os.environ.get('PYDBG', None):
# This code will block execution until you run your Python: Remote Attach
port = 5678
host = 'localhost'
print(f"Blocking for remote attach for vscode {host} {port}")
import debugpy
debugpy.listen((host, port))
debugpy.wait_for_client() You'd need to {
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
],
"justMyCode": true
} |
Beta Was this translation helpful? Give feedback.
-
It will be great if vscode supports something like below. {
"name": "run pipeline",
"request": "launch",
"runtimeArgs": [
"run",
"pipeline"
],
"runtimeVersion": "1.7",
"runtimeExecutable": "poetry",
"envFile": "${workspaceFolder}/.env",
"type": "python",
"cwd": "${workspaceFolder}",
"console": "integratedTerminal"
}, This is similar to what node npm uses. |
Beta Was this translation helpful? Give feedback.
-
I've had success installing Poetry as a development dependency. poetry add --group dev poetry {
"version": "0.2.0",
"configurations": [
{
"name": "poetry install",
"type": "python",
"request": "launch",
"module": "poetry",
"args": [
"-vvv",
"install"
]
},
{
"name": "poetry update",
"type": "python",
"request": "launch",
"module": "poetry",
"args": [
"-vvv",
"update"
]
},
{
"name": "List results",
"type": "python",
"request": "launch",
"module": "poetry",
"args": ["run", "your", "command", "here"]
}
]
} I know you have to be careful where and how you install Poetry, but I haven't had any issues yet. |
Beta Was this translation helpful? Give feedback.
I FOUND A SOLUTION 😄 🥳
Pre requirements
Poetry creates environments in a cache folder.
On my OS (Manjaro), environments are created in
~/.cache/pypoetry/virtualenvs/
.These environments do not have constant names that are easy to identify. For example, my "pizzas" project, has the environment folder defined with the name "pizzas-wQgq9NEZ-py3.10"
So, first of all, this needs to be resolved. We need a more consistent and specific environment path.
What you have to do is find and remove the environment of your project.
In my case I run:
rm -rf ~/.cache/pypoetry/virtualenvs/pizzas-wQgq9NEZ-py3.10
After that, you need to specify a new environment directory:
poetry config virtualenvs.in-project t…