Getting error when i run server.py file
File "C:\Users\nawin\AppData\Local\Programs\Python\Python38\lib\site-packages\starlette\staticfiles.py", line 57, in __init__
raise RuntimeError(f"Directory '{directory}' does not exist")
RuntimeError: Directory 'app/static' does not exist
server.py file
app = Starlette()
app.add_middleware(CORSMiddleware, allow_origins=['*'], allow_headers=['X-Requested-With', 'Content-Type'])
app.mount('/static', StaticFiles(directory='app/static'))
python version 3.8 os windows 10
It's important to known the execution path for your program (for GNU/Linux environment):
If you have a working directory like this: ~/working/myprogram
cd ~/working/myprogrampython3 mysuper.pyThen you can execute without any problems.
But if you are in another directory:
cd ~/Desktoppython3 ~/working/myprogram/mysuper.pyIn this second case you will get the problem. To avoid this, one proposal (is ugly) but can be:
import os
script_dir = os.path.dirname(__file__)
st_abs_file_path = os.path.join(script_dir, "static/")
app.mount("/static", StaticFiles(directory=st_abs_file_path), name="static")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With