FastAPI StaticFiles: 'RuntimeError: Directory static does not exist'
@devsingh05▲ 10 net votes
StaticFiles(directory='static') resolves relative to the current working directory, not to the Python file, so it works from the project root and fails when started from another folder, a Docker WORKDIR, or a test runner. Build an absolute path: BASE = Path(__file__).resolve().parent; app.mount('/static', StaticFiles(directory=BASE / 'static'), name='static'). Create the directory in the image if it is generated at build time.