I want to use FastAPI
inside my python project, I want to deploy it on a Jelastic PaaS.
Apparently mod_wsgi
manages only WSGI application so I was trying to run an ASGI application inside a WSGI application using a2wsgi
like this, this is my wsgi.py
:
import os, sys
virtenv = os.path.expanduser('~') + '/virtenv/'
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
try:
if sys.version.split(' ')[0].split('.')[0] == '3':
exec(compile(open(virtualenv, "rb").read(), virtualenv, 'exec'), dict(__file__=virtualenv))
else:
execfile(virtualenv, dict(__file__=virtualenv))
except IOError:
pass
sys.path.append(os.path.expanduser('~'))
sys.path.append(os.path.expanduser('~') + '/ROOT/')
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
from a2wsgi import ASGIMiddleware
application = ASGIMiddleware(app)
And these lines have been executed for my specific virtenv
:
virtualenv virtenv
source virtenv/bin/activate
pip install a2wsgi
pip install fastapi
deactivate
But it still does not work properly. Probably I'm missing something big. The error doesn't seem "to talk" to me (but I'm not a python-guy):
mod_wsgi (pid=14438): Failed to exec Python script file '/var/www/webroot/ROOT/wsgi.py'.
mod_wsgi (pid=14438): Exception occurred processing WSGI script '/var/www/webroot/ROOT/wsgi.py'.
Traceback (most recent call last):
File "/var/www/webroot/ROOT/wsgi.py", line 24, in <module>
from fastapi import FastAPI
File "/var/www/webroot/virtenv/lib/python3.12/site-packages/fastapi/__init__.py", line 7, in <module>
from .applications import FastAPI as FastAPI
File "/var/www/webroot/virtenv/lib/python3.12/site-packages/fastapi/applications.py", line 16, in <module>
from fastapi import routing
File "/var/www/webroot/virtenv/lib/python3.12/site-packages/fastapi/routing.py", line 22, in <module>
from fastapi import params
File "/var/www/webroot/virtenv/lib/python3.12/site-packages/fastapi/params.py", line 5, in <module>
from fastapi.openapi.models import Example
File "/var/www/webroot/virtenv/lib/python3.12/site-packages/fastapi/openapi/models.py", line 4, in <module>
from fastapi._compat import (
File "/var/www/webroot/virtenv/lib/python3.12/site-packages/fastapi/_compat.py", line 20, in <module>
from fastapi.exceptions import RequestErrorModel
File "/var/www/webroot/virtenv/lib/python3.12/site-packages/fastapi/exceptions.py", line 3, in <module>
from pydantic import BaseModel, create_model
File "/var/www/webroot/virtenv/lib/python3.12/site-packages/pydantic/__init__.py", line 12, in <module>
from . import dataclasses
File "/var/www/webroot/virtenv/lib/python3.12/site-packages/pydantic/dataclasses.py", line 11, in <module>
from ._internal import _config, _decorators, _typing_extra
File "/var/www/webroot/virtenv/lib/python3.12/site-packages/pydantic/_internal/_decorators.py", line 15, in <module>
from ..fields import ComputedFieldInfo
File "/var/www/webroot/virtenv/lib/python3.12/site-packages/pydantic/fields.py", line 19, in <module>
import annotated_types
File "/var/www/webroot/virtenv/lib/python3.12/site-packages/annotated_types/__init__.py", line 361, in <module>
IsNotFinite = Annotated[_NumericType, Predicate(Not(math.isfinite))]
^^^^^^^^^^^^^^^^^^
TypeError: Not() takes no arguments
The full context:
APACHE_VERSION=2.4.57
DOCKER_EXPOSED_PORT=21,22,25,443,7979,80
MOD_WSGI_VERSION=4.9.4
OWASP_MODSECURITY_CRS_VERSION=3.3.2
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PYTHON_VERSION=3.12.0
STACK_VERSION=2.4.57
VERSION=3.12.0
WEBROOT=/var/www/webroot
WSGI_SCRIPT=/var/www/webroot/ROOT/wsgi.py
I have found a simple solution.
I downgraded the jelastic apachepython
container:
2.4.57-python-3.12.0
2.4.57-python-3.9.18
or 2.4.57-python-3.11.6
and it worked.
You can test it on your own Jelastic environment following the steps that I wrote in this public repository on github: danielemaddaluno/pyjel