FastAPI Hello World Example: Internal Server Error

Question:

The Error:

enter image description here

Command Used to Run:

C:UserssuperDesktopWorkCharges2> uvicorn main:app --reload

Console Status:

PS C:UserssuperDesktopWorkCharges2> 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:UserssuperDesktopWorkCharges2venvlibsite-packagesuvicornprotocolshttph11_impl.py", line 364, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "C:UserssuperDesktopWorkCharges2venvlibsite-packagesuvicornmiddlewareproxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "C:UserssuperDesktopWorkCharges2venvlibsite-packagesfastapiapplications.py", line 212, in __call__
    await super().__call__(scope, receive, send)
  File "C:UserssuperDesktopWorkCharges2venvlibsite-packagesstarletteapplications.py", line 119, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:UserssuperDesktopWorkCharges2venvlibsite-packagesstarlettemiddlewareerrors.py", line 181, in __call__
    raise exc
  File "C:UserssuperDesktopWorkCharges2venvlibsite-packagesstarlettemiddlewareerrors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "C:UserssuperDesktopWorkCharges2venvlibsite-packagesstarletteexceptions.py", line 87, in __call__
    raise exc
  File "C:UserssuperDesktopWorkCharges2venvlibsite-packagesstarletteexceptions.py", line 76, in __call__
    await self.app(scope, receive, sender)
  File "C:UserssuperDesktopWorkCharges2venvlibsite-packagesstarletterouting.py", line 659, in __call__
    await route.handle(scope, receive, send)
  File "C:UserssuperDesktopWorkCharges2venvlibsite-packagesstarletteresponses.py", line 50, in __init__
    self.init_headers(headers)
  File "C:UserssuperDesktopWorkCharges2venvlibsite-packagesstarletteresponses.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'

Code (main.py):

from fastapi import FastAPI

app = FastAPI()


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

Folder Structure:

enter image description here

Pip Freeze:

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

Using Python 3.10.2

Asked By: NoName

||

Answers:

I get this error when trying to install your packages with 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

There must be some conflict between your dependencies. Try making a clean install of FastAPI.

If you suspect that there’s a version issue, try installing your requirements from scratch.


If you want to prevent such conflicts in the future, a popular solution is to use a dependency management tool, such as pipenv or poetry.

Answered By: decorator-factory
which uvicorn
which python

I got same error when run Python 3.10.x from virtual environment, but uvicorn was external with Python 3.7. Installing uvicorn to virtialenv solved the problem.

Answered By: dontsov
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.