0

I have Python 3.9 (/usr/bin/python3.9) with Django 3.1.6 and with Apache2 want to run my application.

But on the browser is error 500 Internal Server Error and in log of Apache I'm getting SyntaxError in datetime.py:

mod_wsgi (pid=25852): Target WSGI script '/path/to/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=25852): Exception occurred processing WSGI script '/path/to/wsgi.py'.
Traceback (most recent call last):
  File "/path/to/wsgi.py", line 12, in <module>
    from django.core.wsgi import get_wsgi_application
  File "/usr/lib/python3.9/site-packages/django/__init__.py", line 1, in <module>
    from django.utils.version import get_version
  File "/usr/lib/python3.9/site-packages/django/utils/version.py", line 1, in <module>
    import datetime
  File "/usr/lib/python3.9/datetime.py", line 889
    raise ValueError(f'Invalid isoformat string: {date_string!r}')
                                                                ^
SyntaxError: invalid syntax

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Martin Special
  • 141
  • 1
  • 7
  • 1
    I don't have any experience with Django, so I don't know how this would happen, but it looks like a Python 2 interpreter is attempting to use a Python 3 library. Do you configure what interpreter to use somewhere, separate from where the library folder is specified? – Carcigenicate Feb 16 '21 at 15:31
  • You could double check the file that it's indicating too (`datetime.py`). If it was corrupted somehow, there may be a `)` or something missing on one of the lines above the error line. – Carcigenicate Feb 16 '21 at 15:35
  • Could you share with us any datetime.py code, without that there's nothing to check – EngineJanwaar Feb 16 '21 at 15:48
  • Interesting idea, i just don't know where python 2 would appear. In Apache config I have set `python-path=/usr/bin/python3.9:/usr/lib/python3.9/site-packages:/usr/lib/python3.9`. I also set `sudo ln -s python3.9 python` but still the same. – Martin Special Feb 16 '21 at 15:54
  • I checked the file for corruption and most likely, the `datetime.py` file is fine. When I try `import datetime` in python shell, everything is ok. – Martin Special Feb 16 '21 at 15:59
  • 1
    To be able to use Python 3.9 with `mod_wsgi`, you **need to compile `mod_wsgi` using 3.9**. It seems `mod_wsgi` was compiled using an older Python version and that's what it's using. See docs: https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html#virtual-environment-and-python-version – xyres Feb 16 '21 at 17:13
  • Solution: ```sudo apt-get remove libapache2-mod-wsgi-py3```. Reinstall Python3.9 with `./configure --enable-optimizations --prefix=/usr --enable-shared`. ```sudo -H pip3.9 install mod_wsgi``` ; ```mod_wsgi-express module-config >> /etc/apache2/apache2.conf``` ; ```sudo systemctl reload apache2``` – Martin Special Feb 16 '21 at 20:21
  • Please don't put the answer in the question. Your question is already linked to this Q&A pair: https://stackoverflow.com/q/44914961/2745495. If you have better solution (looks like you do have more complete steps), then you can post that as a new answer on the linked Q&A pair. – Gino Mempin Feb 17 '21 at 00:19

0 Answers0