2

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

Navin pari
  • 21
  • 1
  • 1
  • 4

1 Answers1

5

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

First case

  1. First access to your working directory: cd ~/working/myprogram
  2. Execute: python3 mysuper.py

Then you can execute without any problems.

Second case

But if you are in another directory:

  1. Go to Desktop folder: cd ~/Desktop
  2. Execute: python3 ~/working/myprogram/mysuper.py

In 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")
Dharman
  • 30,962
  • 25
  • 85
  • 135
fquinto
  • 527
  • 7
  • 12