full-stack-fastapi-template icon indicating copy to clipboard operation
full-stack-fastapi-template copied to clipboard

How to debug inside pycharm ?

Open masoodkamyab opened this issue 1 year ago • 2 comments

Hi to every one, I want to debug with PyCharm debugger run my tests and see all variables and traces. but it seems it didn't load the .env file so I have an error that some critical environment variable is missing (note that I don't want to use docker to run my tests). here some error that I got:

ImportError while loading conftest '/home/has/project/backend/app/app/tests/conftest.py'.
../conftest.py:7: in <module>
    from app.core.config import settings
../../core/config.py:90: in <module>
    settings = Settings()
pydantic/env_settings.py:39: in pydantic.env_settings.BaseSettings.__init__
    ???
pydantic/main.py:341: in pydantic.main.BaseModel.__init__
    ???
E   pydantic.error_wrappers.ValidationError: 11 validation errors for Settings
E   SERVER_NAME
E     field required (type=value_error.missing)
E   SERVER_HOST
E     field required (type=value_error.missing)
E   PROJECT_NAME
E     field required (type=value_error.missing)
E   SENTRY_DSN
E     object of type 'NoneType' has no len() (type=type_error)
E   POSTGRES_SERVER
E     field required (type=value_error.missing)
E   POSTGRES_USER
E     field required (type=value_error.missing)
E   POSTGRES_PASSWORD
E     field required (type=value_error.missing)
E   POSTGRES_DB
E     field required (type=value_error.missing)
E   SQLALCHEMY_DATABASE_URI
E     can only concatenate str (not "NoneType") to str (type=type_error)
E   FIRST_SUPERUSER
E     field required (type=value_error.missing)
E   FIRST_SUPERUSER_PASSWORD
E     field required (type=value_error.missing)

If anybody can help me I will appreciate it. thanks

masoodkamyab avatar May 25 '23 12:05 masoodkamyab

I also have this problem, this framework is currently unfriendly to collaborative development, everyone should not run a docker for development

MaxwellEdisons avatar Jul 18 '23 13:07 MaxwellEdisons

You can use the EnvFile plugin so that you can explicitly tell PyCharm to load an env file in a run configuration. Very useful. https://plugins.jetbrains.com/plugin/7861-envfile

SpoonOfDoom avatar Nov 14 '23 14:11 SpoonOfDoom

I got it working like this (using PyCharm Professional 2024.1.3 )

Docker Connection

  • Navigate to PyCharm Settings > Build, Execution, Deployment > Docker
  • Verify connection to a running Docker Engine

Project Interpreter Setup

  • Go to PyCharm Settings > Project: your-project > Project Interpreter
    • Add new interpreter:
    • Select "On Docker Compose..."
    • Choose the verified Docker server
    • Select your fastapi service
    • Click next, ignore any errors
    • Choose "System Interpreter" (/usr/local/bin/python3)

Run Configuration

  • Create a new "FastAPI" configuration
  • Settings:
    • Application file: main.py
    • Uvicorn options: --reload --host 0.0.0.0 --port YOUR_PORT
    • Python interpreter: Remote Python 3.xx Docker Compose (your fastapi service)

davidhuser avatar Aug 07 '24 08:08 davidhuser