Search code examples
pythonflaskmod-wsgiwerkzeug

AttributeError: module 'typing' has no attribute 'Literal'


I have python 3.9 installed in my windows machine with a flask script working as expected. Then I tried to replicate the environment on my linux machine. I have the same python version and the exact same pip list. But in the linux machine I receive this error from mod_wsgi:

from flask import Flask, abort
File "/var/www/VirtualEnvs/newEnv/lib/python3.9/site-packages/flask/__init__.py", line 1, in <module>
from . import json as json
File "/var/www/VirtualEnvs/newEnv/lib/python3.9/site-packages/flask/json/__init__.py", line 6, in <module>
from ..globals import current_app
File "/var/www/VirtualEnvs/newEnv/lib/python3.9/site-packages/flask/globals.py", line 6, in <module>
from werkzeug.local import LocalProxy
File "/var/www/VirtualEnvs/newEnv/lib/python3.9/site-packages/werkzeug/__init__.py", line 1, in <module>
from .serving import run_simple as run_simple
File "/var/www/VirtualEnvs/newEnv/lib/python3.9/site-packages/werkzeug/serving.py", line 76, in <module>
t.Union["ssl.SSLContext", t.Tuple[str, t.Optional[str]], t.Literal["adhoc"]]
AttributeError: module 'typing' has no attribute 'Literal'

I even tried upgrading to python3.10 and I have the same result:

from flask import Flask, abort
File "/usr/local/lib/python3.10/dist-packages/flask/__init__.py", line 5, in <module>
from . import json as json
File "/usr/local/lib/python3.10/dist-packages/flask/json/__init__.py", line 6, in <module>
from ..globals import current_app
File "/usr/local/lib/python3.10/dist-packages/flask/globals.py", line 6, in <module>
from werkzeug.local import LocalProxy
File "/usr/local/lib/python3.10/dist-packages/werkzeug/__init__.py", line 5, in <module>
from .serving import run_simple as run_simple
File "/usr/local/lib/python3.10/dist-packages/werkzeug/serving.py", line 76, in <module>
t.Union["ssl.SSLContext", t.Tuple[str, t.Optional[str]], t.Literal["adhoc"]]
AttributeError: module 'typing' has no attribute 'Literal'

My last idea was to pip install --upgrade typing which gave me this new error:

from flask import Flask, abort
File "/usr/local/lib/python3.10/dist-packages/flask/__init__.py", line 3, in <module>
import typing as t
File "/usr/local/lib/python3.10/dist-packages/typing.py", line 1359, in <module>
class Callable(extra=collections_abc.Callable, metaclass=CallableMeta):
File "/usr/local/lib/python3.10/dist-packages/typing.py", line 1007, in __new__
self._abc_registry = extra._abc_registry
AttributeError: type object 'Callable' has no attribute '_abc_registry'

As far as I know, this pip install --upgrade typing should not be used in this python version. Here more details could be found.

Flask version 3.0.2 Werkzeug version 3.0.1


Solution

  • Found the issue. My mod-wsgi was compiled against python3.7. So any virtualenv with different python version won't work.

    I solved it by downloading last mod-wsgi version and compiling it against the same python3.9 as my virtualenv (./configure --with-python=/path/to/python)