python config_error ai_generated true

FileNotFoundError: [Errno 2] No such file or directory: 'static/style.css'

ID: python/starlette-missing-static-files

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-12-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

Starlette static file mount expects the directory to exist; if the file is missing or path is incorrect, this error occurs.

generic

中文

Starlette 静态文件挂载要求目录存在;如果文件丢失或路径错误,则会发生此错误。

Workarounds

  1. 100% success Create the static directory and place files
    mkdir -p static
    # place style.css in static/
  2. 95% success Mount static files with correct path
    from starlette.staticfiles import StaticFiles
    app.mount('/static', StaticFiles(directory='static'), name='static')

Dead Ends

Common approaches that don't work:

  1. Using a relative path that doesn't exist 90% fail

    The path must be absolute or relative to the current working directory.

  2. Not mounting the static files at all 100% fail

    Static files won't be served.