运行server.py文件时出错
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文件
app = Starlette()
app.add_middleware(CORSMiddleware, allow_origins=['*'], allow_headers=['X-Requested-With', 'Content-Type'])
app.mount('/static', StaticFiles(directory='app/static'))
python版本3.8os windows 10
发布于 2021-10-01 07:14:52
了解程序的执行路径(对于GNU/Linux环境)是很重要的:
如果您有这样一个工作目录:~/working/myprogram
第一例
第一次访问您的工作目录:cd ~/working/myprogram
python3 mysuper.py
然后,您可以执行没有任何问题。
第二例
但是,如果您位于另一个目录中:
cd ~/Desktop
python3 ~/working/myprogram/mysuper.py
在第二种情况下,您将遇到问题。为了避免这种情况,有一项建议(很难看),但可以是:
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")
https://stackoverflow.com/questions/66513425
复制相似问题