poetry icon indicating copy to clipboard operation
poetry copied to clipboard

VSCode + debugging + poetry

Open TimoFriedri opened this issue 4 years ago • 13 comments

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 poetry run command, but in a poetry shell with python main.py, the debugging works flawlessly.

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 Poetry (systemwide): 1.1.13 Poetry python: poetry: 1.1.13 poetry-core: 1.0.8

  • [ x] I have searched the issues of this repo and believe that this is not a duplicate.
  • [ x] I have searched the documentation and believe that my question is not covered.

TimoFriedri avatar Mar 24 '22 14:03 TimoFriedri

@finswimmer I have seen you answered on another issue as poetry dev.

This might be related? https://youtrack.jetbrains.com/issue/PY-52864

TimoFriedri avatar Mar 28 '22 08:03 TimoFriedri

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"
            ]
        }
    ]
}

lucasvazq avatar Jul 08 '22 12:07 lucasvazq

I FOUND A SOLUTION :smile: :partying_face:

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 true

This will create a new .venv file within your project. You can ignore it with .gitignore.

Because we created a new environment, we have to install the dependencies again. Execute the following command at the root of your project:

poetry install

Config

With 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"
            ]
        }
    ]
}

lucasvazq avatar Jul 08 '22 14:07 lucasvazq

@lucasvazq your solution solve my problems, congrats bro!!!

DaviYokogawa avatar Jul 12 '22 04:07 DaviYokogawa

thank you bro

kyo219 avatar Dec 08 '22 08:12 kyo219

I my case, @lucasvazq solution worked with just the launch.json config without "pythonPath", I didn't have to remove the environment nor modify poetry config. Thanks!

hielfx avatar Mar 08 '23 08:03 hielfx

@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 launch.json a bit. Maybe that's helpful for others, too?

{
    "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,
	}
    ]
}

aljoshakoecher avatar Mar 16 '23 09:03 aljoshakoecher

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

You can also use poetry to manage the environments:

poetry env list  # shows the name of the current environment
poetry env remove <current environment>
poetry install  # will create a new environment using your updated configuration

source

manmorjim avatar Jul 06 '23 08:07 manmorjim

Another way to resolve this is to make your package have a module by creating __main__.py. It can be something as simple as:

project/
├── __init__.py
├── __main__.py
└── project.py
# __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 launch.json:

{
  "configurations": [
    {
      "args": ["-v"],
      "console": "integratedTerminal",
      "module": "project",
      "name": "project -v",
      "request": "launch",
      "showReturnValue": true,
      "type": "python"
    }
  ],
  "version": "0.2.0"
}

Tatsh avatar Jul 14 '23 11:07 Tatsh

In my current test (as of Sep 25th 2023), below is still valid but we don't need to set this config anymore:

poetry config virtualenvs.in-project true

Also we don't need to set the "pythonPath" in the setting anymore (as VSCode does not support that anymore?).

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 true

This will create a new .venv file within your project. You can ignore it with .gitignore.

Because we created a new environment, we have to install the dependencies again. Execute the following command at the root of your project:

poetry install

Config

With 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"
            ]
        }
    ]
}

caopuzheng avatar Sep 26 '23 01:09 caopuzheng

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 poetry run python discogs.py "I've Got a Feeling", "The Beatles" would be but in debug mode.

yorkshirelandscape avatar Oct 01 '23 23:10 yorkshirelandscape

The only clean way I've found is to use debugpy and attach to your process via the 'Python: Remote Attach' configuration in your launch.json. You would run your script from the command line and then you attach to it.

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 poetry add --group dev debugpy first

        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "."
                }
            ],
            "justMyCode": true
        }

charterchap avatar Dec 12 '23 22:12 charterchap

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.

praveer-k avatar Jan 24 '24 09:01 praveer-k