Cannot execute FastAPI client code in Spyder

Viewed 347

While trying to develop a FastAPI client in Spyder environment, one cannot start the execution.

The code fails and raises:

RuntimeError: asyncio.run() cannot be called from a running event loop Minimum code to be executed:

from fastapi import FastAPI
import uvicorn

app = FastAPI()


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


if __name__ == '__main__':
    uvicorn.run(app, port=8080, host='0.0.0.0')
1 Answers

(Spyder maintainer here) If you want to run applications that use asyncio in Spyder, you need to install a package called nest_asyncio first and then run the following code in the IPython console before running your code:

import nest_asyncio
nest_asyncio.apply()

Note: You only need to run that code once.

Related