fastapi / full-stack-fastapi-template

Full stack, modern web application template. Using FastAPI, React, SQLModel, PostgreSQL, Docker, GitHub Actions, automatic HTTPS and more.
MIT License
27.22k stars 4.86k forks source link

How to debug backend in Pycharm or VSCode #242

Open JohnnyFee opened 4 years ago

JohnnyFee commented 4 years ago

I run the project with docker successfully, but I want to know how can I debug backend in PyCharm or VSCode step by step?

JohnnyFee commented 4 years ago

I checked #7 , but no satisfied answer. Can anyone provide best practice for debugging?

daniel-butler commented 4 years ago

How would you normally debug something, what does that look like?

If you create tests then run the debugger through them I'd suggest setting up pytest integration and using pycharm's professional version connect it to the docker compose interpreter. A Google search for the keywords should get you there.

kisscelia commented 4 years ago

I run the backend individually. Steps as following.

  1. copy backend to your own directory.
  2. add the code in the main.py:
    if __name__ == "__main__":
    import uvicorn
    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=settings.SERVER_PORT,
        log_level="debug",
        reload=True,
        workers=1
    )
  3. modify the config.py: add values to the variables(without value default) e.g.:
    
    SERVER_NAME: str = ""
    SERVER_HOST: AnyHttpUrl = "http://127.0.0.1:10088"
    PROJECT_NAME: str = "proName"
    SENTRY_DSN: Optional[HttpUrl] = ""  # None --> ""

POSTGRES_SERVER: str = "postgres ip" POSTGRES_USER: str = "database user" POSTGRES_PASSWORD: str = "user password" POSTGRES_DB: str = "db name"

SMTP_PORT: Optional[int] = smtp port with ssl (maybe 465) SMTP_HOST: Optional[str] = "smtp server or others" SMTP_USER: Optional[str] = "your email" SMTP_PASSWORD: Optional[str] = "your password" EMAILS_FROM_EMAIL: Optional[EmailStr] = "your email" EMAILS_FROM_NAME: Optional[str] = "your name"

FIRST_SUPERUSER: EmailStr = "your email for admin" FIRST_SUPERUSER_PASSWORD: str = "your password"


4. run main.py  --- the project maybe works.
TheCDC commented 3 years ago

I was able to get it working based on this repo: https://github.com/Kludex/fastapi-docker-debug

I can restart the backend container in debug mode and connect to the session via VS Code.

Launch.json

{
  "configurations": [
    {
      "name": "Python: Remote Attach",
      "type": "python",
      "request": "attach",
      "port": 5678,
      "host": "localhost",
      "pathMappings": [
        {
          "localRoot": "${workspaceFolder}/app",
          "remoteRoot": "/app"
        }
      ]
    }
  ]
}

docker-compose.debug.yml

version: "3.3"
services:
  backend:
    environment:
      - SERVER_NAME=${DOMAIN?Variable not set}
      - SERVER_HOST=https://${DOMAIN?Variable not set}
      # Allow explicit env var override for tests
      - SMTP_HOST=${SMTP_HOST}    
    env_file:
      - .env
    build:
      context: ./backend
      dockerfile: backend.dockerfile
      args:
        INSTALL_DEV: ${INSTALL_DEV-false}
    command:
      [
        "sh",
        "-c",
        "pip install debugpy && python -m debugpy --wait-for-client --listen 0.0.0.0:5678 -m uvicorn app.main:app --reload --host 0.0.0.0 --port 8000",
      ]
    volumes:
      - ./backend/app:/app
    ports:
      - 8000:8000
      - 5678:5678

networks:
  traefik-public:
    # For local dev, don't expect an external Traefik network
    external: false

Command to run backend in debug mode

docker-compose -f docker-compose.debug.yml up

NMisko commented 3 years ago

For PyCharm see https://github.com/tiangolo/full-stack-fastapi-postgresql/issues/7#issuecomment-822730879