Skip to content

Instantly share code, notes, and snippets.

@timvw
Created September 10, 2024 12:34
Show Gist options
  • Save timvw/27b1abb740fbf6ff2878e42bd74eaab9 to your computer and use it in GitHub Desktop.
Save timvw/27b1abb740fbf6ff2878e42bd74eaab9 to your computer and use it in GitHub Desktop.
Entrypoint for FastAPI/uvicorn application executed as python script
import uvicorn
if __name__ == "__main__":
'''
Entrypoint to run the FastAPI application (without using the FastAPI/Uvicorn CLI).
Because a python script can't import relatively, we apply a sys.path hack to import the application.
Typically you do not need this and simply invoke one of the following:
* rye run fastapi dev ./src/backend/app.py
* fastapi run dev ./src/backend/app.py
'''
import os
import sys
sys.path.append(os.path.abspath('../'))
from backend.app import app
uvicorn.run(app, host="0.0.0.0", port=8000)
@timvw
Copy link
Author

timvw commented Sep 10, 2024

PyCharm has a FastAPI run/debug configuration template...
Screenshot 2024-09-10 at 14 40 48

@timvw
Copy link
Author

timvw commented Sep 10, 2024

VsCode also has a FastAPI debug configuration
Screenshot 2024-09-10 at 14 43 43

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