FastAPI Hello World 示例:内部服务器错误

FastAPI Hello World Example: Internal Server Error

错误:

用于运行的命令:

C:\Users\super\Desktop\Work\Charges2> uvicorn main:app --reload

控制台状态:

PS C:\Users\super\Desktop\Work\Charges2> uvicorn main:app --reload
INFO:     Will watch for changes in these directories: ['C:\Users\super\Desktop\Work\Charges2']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [69628] using statreload
WARNING:  The --reload flag should not be used in production on Windows.
INFO:     Started server process [72184]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     127.0.0.1:61648 - "GET / HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "C:\Users\super\Desktop\Work\Charges2\venv\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 364, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "C:\Users\super\Desktop\Work\Charges2\venv\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "C:\Users\super\Desktop\Work\Charges2\venv\lib\site-packages\fastapi\applications.py", line 212, in __call__
    await super().__call__(scope, receive, send)
  File "C:\Users\super\Desktop\Work\Charges2\venv\lib\site-packages\starlette\applications.py", line 119, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\super\Desktop\Work\Charges2\venv\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
    raise exc
  File "C:\Users\super\Desktop\Work\Charges2\venv\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "C:\Users\super\Desktop\Work\Charges2\venv\lib\site-packages\starlette\exceptions.py", line 87, in __call__
    raise exc
  File "C:\Users\super\Desktop\Work\Charges2\venv\lib\site-packages\starlette\exceptions.py", line 76, in __call__
    await self.app(scope, receive, sender)
  File "C:\Users\super\Desktop\Work\Charges2\venv\lib\site-packages\starlette\routing.py", line 659, in __call__
    await route.handle(scope, receive, send)
  File "C:\Users\super\Desktop\Work\Charges2\venv\lib\site-packages\starlette\responses.py", line 50, in __init__
    self.init_headers(headers)
  File "C:\Users\super\Desktop\Work\Charges2\venv\lib\site-packages\starlette\responses.py", line 77, in init_headers
    and not (self.status_code < 200 or self.status_code in (204, 304))
TypeError: '<' not supported between instances of 'NoneType' and 'int'

代码(main.py):

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

文件夹结构:

点冻结:

anyio==3.5.0
asgiref==3.5.0
click==8.0.3
colorama==0.4.4
fastapi==0.73.0
h11==0.13.0
idna==3.3
pydantic==1.9.0
sniffio==1.2.0
starlette==0.18.0
typing_extensions==4.1.1
uvicorn==0.17.4

使用 Python 3.10.2

我在尝试使用 pip install -r requirements.txt:

安装您的软件包时遇到此错误
ERROR: Cannot install -r requirements.txt (line 5) and starlette==0.18.0
because these package versions have conflicting dependencies.

The conflict is caused by:
The user requested starlette==0.18.0
fastapi 0.73.0 depends on starlette==0.17.1

你的依赖项之间肯定存在一些冲突。尝试全新安装 FastAPI。

如果您怀疑存在版本问题,请尝试从头开始安装您的要求。


如果你想在未来防止此类冲突,一个流行的解决方案是使用依赖管理工具,例如pipenv or poetry