tiangolo / fastapi

FastAPI framework, high performance, easy to learn, fast to code, ready for production
https://fastapi.tiangolo.com/
MIT License
73.13k stars 6.16k forks source link

app.mout not working while packaging fastapi application using pyinstaller #11707

Closed nitishroy6 closed 3 weeks ago

nitishroy6 commented 3 weeks ago

Privileged issue

Issue Content

`app = FastAPI() base_dir = os.path.dirname(os.path.abspath(file))

Full paths for static and templates directories

static_dir = os.path.join(base_dir, "app/static") templates_dir = os.path.join(base_dir, "app/templates")

Mount the 'static' directory to serve static files

app.mount("/static", StaticFiles(directory=static_dir), name="static") app.mount("/templates", StaticFiles(directory=templates_dir), name="templates")

app.include_router(auths.auth)

if name == "main": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)`

Application is running well through uvicorn or python main.py or fastapi dev

but i am trying to make build of my application through pyinstaller (command is below) pyinstaller -F --onefile main.py

and when i am commenting the app.mount lines then the build is working fine, only it is giving below error for app.mout

PS C:\Users\Nitish Kumar\abc\cas-apis\dist> .\main.exe Database connection is active. Traceback (most recent call last): File "main.py", line 84, in <module> File "starlette\staticfiles.py", line 59, in __init__ RuntimeError: Directory 'app/static' does not exist [3076] Failed to execute script 'main' due to unhandled exception! PS C:\Users\Nitish Kumar\abc\cas-apis\dist> .\main.exe Database connection is active. Traceback (most recent call last): File "main.py", line 90, in <module> File "starlette\staticfiles.py", line 59, in __init__ RuntimeError: Directory 'C:\Users\NITISH~1\AppData\Local\Temp\_MEI144162\app/static' does not exist [9040] Failed to execute script 'main' due to unhandled exception! PS C:\Users\Nitish Kumar\abc\cas-apis\dist>

i checked everywhere like fastapi docs, google, chatgpt , still didn't get solution