2

I have a Django app I've been working on debugging using manage.py runserver.

I'm now attempting to serve it with apache/mod_wsgi.

When I try to open the home page I received a 500 error. The log file shows a syntax error in functools.py.

mod_wsgi (pid=28016): Target WSGI script '/home/username/projects/django/testing/testing/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=28016): Exception occurred processing WSGI script '/home/username/projects/django/testing/testing/wsgi.py'.
Traceback (most recent call last):
File "/home/username/projects/django/testing/testing/wsgi.py", line 12, in <module>
  from django.core.wsgi import get_wsgi_application
File "/home/username/.virtualenvs/testing/lib/python3.4/site-packages/django/__init__.py", line 1, in <module>
  from django.utils.version import get_version
File "/home/username/.virtualenvs/testing/lib/python3.4/site-packages/django/utils/version.py", line 7, in <module>
  from django.utils.lru_cache import lru_cache
File "/home/username/.virtualenvs/testing/lib/python3.4/site-packages/django/utils/lru_cache.py", line 2, in <module>
  from functools import lru_cache
File "/home/username/.virtualenvs/testing/lib/python3.4/functools.py", line 291
  cls_or_self, *rest = args
               ^
SyntaxError: invalid syntax

As far as I can tell that is valid python3.4 syntax. So I'm not entirely sure if I have WSGI configured correctly to use my virtualenv.

#testing.conf
<VirtualHost *:80>
    WSGIDaemonProcess django.testing.username.vni user=username group=username python-path=/home/username/.virtualenvs/testing/lib/python3.4/site-packages:/home/username/.virtualenvs/testing/lib/python3.4:/home/username/.virtualenvs/testing
    WSGIProcessGroup django.testing.username.vni

    WSGIScriptAlias / /home/username/projects/django/testing/testing/wsgi.py

    ServerAdmin [email protected]
    ServerName django.testing
    ServerAlias www.django.testing

    Alias /static/ /home/username/projects/django/testing/static/

    <Directory />
        AllowOverride None
        Options -Indexes
    </Directory>

    <Directory /home/username/projects/django/testing/testing>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    ErrorLog /var/log/apache2/django.testing.log
    LogLevel warn

</VirtualHost>

I think I might have a problematic python-path above.

#wsgi.py
import os

from django.core.wsgi import get_wsgi_application

import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/..")
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testing.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

application = get_wsgi_application()

I'm not sure if I've handled the sys.path.append()'s properly or the DJANGO_SETTINGS_MODULE. They are as they are to hack around a few other errors I ran into.

4
  • 2
    How did you install mod_wsgi? It seems that you are running a 3.4 project with a mod_wsgi compiled for 2.7. Commented May 11, 2015 at 19:42
  • I think I just apt-get install libapache2-mod-wsgi after a brief search it looks like I should have gotten libapache2-mod-wsgi-py3 instead? Commented May 11, 2015 at 19:50
  • 1
    Depending on your distro/version, you may have to compile a 3.3+ Python mod_wsgi. That's what we had to do for CentOS 6.5 / RHEL6 (yes, ugh), because it wasn't available through yum. If the '-py3' version is available as a package, give it a try first. Commented May 13, 2015 at 2:12
  • @DanielRoseman: how did you see that!? Is there something in the error info that I'm missing, or do you need intuition for that? Thanks a lot, by the way :) Commented Feb 6, 2017 at 17:04

1 Answer 1

4

As suggested by Daniel Roseman, the issue was that I had the wrong version of mod_wsgi for python3.4. I removed libapache2-mod-wsgi and installed libapache2-mod-wsgi-py3.

Sign up to request clarification or add additional context in comments.

1 Comment

you are my HERO, i searched sooo long for that (for a different importing problem). Please accept your answer, then it will be easier to find (I suppose, at least^^)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.