[go: nahoru, domu]

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 backend in Pycharm or VSCode #242

Open
JohnnyFee opened this issue Aug 5, 2020 · 5 comments
Open

How to debug backend in Pycharm or VSCode #242

JohnnyFee opened this issue Aug 5, 2020 · 5 comments

Comments

@JohnnyFee
Copy link
JohnnyFee commented Aug 5, 2020

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 JohnnyFee changed the title How to debug in Pycharm or VsCode How to debug backend in Pycharm or VSCode Aug 5, 2020
@JohnnyFee
Copy link
Author

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

@daniel-butler
Copy link
Contributor

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

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
    )
  1. 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"
  1. run main.py --- the project maybe works.

@TheCDC
Copy link
TheCDC commented Jan 15, 2021

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
Copy link
NMisko commented Apr 19, 2021

For PyCharm see #7 (comment)

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

No branches or pull requests

6 participants