Attempting to run a FastAPI application on CentOS 7 but getting a module 'asyncio' error

Question:

I am attempting to run a simple FastAPI application on CentOS 7 but getting some errors. I will include some more details for context:

Python Version – 3.6.8
pip version – 9.0.3

I am running the application with this command: python3 -m uvicorn main:app

I keep getting this error:

Traceback (most recent call last):
  File "/usr/lib64/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib64/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.6/site-packages/uvicorn/__main__.py", line 4, in <module>
    uvicorn.main()
  File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.6/site-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/uvicorn/main.py", line 435, in main
    run(app, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/uvicorn/main.py", line 461, in run
    server.run()
  File "/usr/local/lib/python3.6/site-packages/uvicorn/server.py", line 67, in run
    return asyncio.run(self.serve(sockets=sockets))
AttributeError: module 'asyncio' has no attribute 'run'

I was initially getting this error – /home/centos/fast_api/fastapi-tutorial/python3-venv/bin/python3: No module named uvicorn but after installing uvicorn via pip3 install uvicorn, I now get the "module ‘asyncio’ error"

Any help would be great

I have tried enabling a python virtual environment on the server but I still get the same error.

Could this be an issue with the Python version?

Asked By: Kelvin Lawson

||

Answers:

FastAPI requires Python 3.7+, it’s not compatible with your interpreter.

In your case the app fails because the asyncio.run() function was added in Python 3.7.

Answered By: Oleh Rybalchenko

Update on this. I was able to get this working by upgrading both python and pip.

I upgraded python to 3.9

Upgraded pip using – /usr/local/bin/python3.9 -m pip install --upgrade pip

Thanks for the help.

Answered By: Kelvin Lawson
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.