Skip to content
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

How to Debug using vs code #503

Open
fatheyabdelslam opened this issue Mar 26, 2023 · 5 comments
Open

How to Debug using vs code #503

fatheyabdelslam opened this issue Mar 26, 2023 · 5 comments

Comments

@fatheyabdelslam
Copy link

i'm using this repo for my next project,
but i can't figure out a way for debugging in vs code or connect jupyter server with vs code,
i want to be able to set breakpoints and watch call sack .. etc.

@miguelvalente
Copy link

To run a debugger dockerless do the following.

Create a new file

|-- backend
|   |-- app
|      |-- app
|      |-- alembic
|      |run.py           <--------- Create this file

run.py

import uvicorn

if __name__ == "__main__":
    uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True)

Use default launch

        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true
        }

Let me know if works.

@MaxwellEdisons
Copy link

But the environment variable cannot be obtained

@davidhuser
Copy link

@MaxwellEdisons you could re-load them again with dotenv (which should already be present as it used by pydantic)

import uvicorn
from dotenv import load_dotenv

load_dotenv("../.env")

if __name__ == "__main__":
    uvicorn.run("app.main:app", host="0.0.0.0", port=8002, reload=True)

@AkashKobal
Copy link

Reload dotenv using load_dotenv and reload = True while calling run method

import uvicorn
from dotenv import load_dotenv

load_dotenv("./.env")

if __name__ == "__main__":
    uvicorn.run("app.main:app", host="yout_local_host", port=your_port_no, reload=True)

@QDFish
Copy link

QDFish commented Sep 25, 2024

I add the below code in backend/app/main.py

if settings.ENVIRONMENT == 'local':
    import debugpy
    print("listening")
    debugpy.listen(("0.0.0.0", 5678))
    print("wait for client")
    debugpy.wait_for_client()

adding attach action in launch.json

        {
            "name": "Python attach",
            "type": "debugpy",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}/backend",
                    "remoteRoot": "/app"
                }
            ],
            "justMyCode": false,            
        }

and don't forget to add dependency 'debugpy' in the pyproject.toml

now, you can rebuild the compose file and you will see the log 'wait for client' then click the attach action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants