问题
I have set up a Django site with MongoEngine and I am trying to run it using Apache and mod_wsgi with a virtualenv.
It runs fine in development, but under Apache (on ubuntu 14.04.2) I get the following errors in the apache log:
mod_wsgi (pid=16130): Exception occurred processing WSGI script '/data/.../.../wsgi.py'.
Traceback (most recent call last):
File "/data/.../.../wsgi.py", line 22, in application
return get_wsgi_application()(environ, start_response)
File "/data/venv/lib/python3.4/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
django.setup()
File "/data/venv/lib/python3.4/site-packages/django/__init__.py", line 17, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/data/venv/lib/python3.4/site-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/data/venv/lib/python3.4/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/data/venv/lib/python3.4/site-packages/django/conf/__init__.py", line 92, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/data/.../.../settings.py", line 15, in <module>
import mongoengine
File "/data/venv/lib/python3.4/site-packages/mongoengine/__init__.py", line 1, in <module>
from . import document
File "/data/venv/lib/python3.4/site-packages/mongoengine/document.py", line 51
class EmbeddedDocument(BaseDocument, metaclass=DocumentMetaclass):
^
SyntaxError: invalid syntax
Any ideas why this might occur? This is particularly strange to me as I can't find the offending line in the mongoengine source.
My packages are:
dj-database-url==0.3.0
dj-static==0.0.6
Django==1.8
django-toolbelt==0.0.1
gunicorn==19.3.0
mongoengine==0.9.0
psycopg2==2.6
pymongo==2.8
static3==0.5.1
I'm not sure if it's related, but why is it using importlib
from python2.7
and not python3.4
?
My copy of mongoengine/document.py (installed using pip install
) does not match the version in git, and has the following:
class EmbeddedDocument(BaseDocument, metaclass=DocumentMetaclass):
...
# The __metaclass__ attribute is removed by 2to3 when running with Python3
# my_metaclass is defined so that metaclass can be queried in Python 2 & 3
I'm new to python so I don't understand this, but it seems like a python 3 incompatibility?
回答1:
The problem was that I was using the ubuntu libapache2-mod-wsgi
package version of mod_wsgi
which is built for Python 2.7 but my virtualenv was built around python 3.4.
The issue was resolved by uninstalling libapache2-mod-wsgi
and installing libapache2-mod-wsgi-py3
which is built for Python 3.4.
来源:https://stackoverflow.com/questions/29489883/error-using-mod-wsgi-django-and-mongoengine