Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VSCode + debugging + poetry #5354

Closed
TimoFriedri opened this issue Mar 24, 2022 · 13 comments
Closed

VSCode + debugging + poetry #5354

TimoFriedri opened this issue Mar 24, 2022 · 13 comments

Comments

@TimoFriedri
Copy link

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
Copy link
Author

TimoFriedri commented Mar 28, 2022

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

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

@lucasvazq
Copy link

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
Copy link

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

@DaviYokogawa
Copy link

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

@kyo219
Copy link

kyo219 commented Dec 8, 2022

thank you bro

@hielfx
Copy link

hielfx commented Mar 8, 2023

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!

@aljoshakoecher
Copy link

@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,
	}
    ]
}

@manmorjim
Copy link

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

@Tatsh
Copy link

Tatsh commented Jul 14, 2023

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

@caopuzheng
Copy link

caopuzheng commented Sep 26, 2023

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

@yorkshirelandscape
Copy link

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.

@charterchap
Copy link

charterchap commented Dec 12, 2023

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
        }

@praveer-k
Copy link

praveer-k commented Jan 24, 2024

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.

@python-poetry python-poetry locked and limited conversation to collaborators Mar 23, 2024
@abn abn converted this issue into discussion #9206 Mar 23, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests