SyntaxError with VirtualEnv + mod-wsgi in Django

2019-03-05 04:30发布

问题:

I am having trouble using VirtualEnv on my production Ubuntu 13.04 server with mod-wsgi.

Would someone be able to point out what the issue may be?

Here is the traceback that I am getting the below syntax error:

[Mon Jul 14 14:37:09 2014] [error] [client 70.180.246.22] mod_wsgi (pid=14292): Target WSGI script '/home/aaron/public_html/flapsta.com/flapsta/flapsta/flapsta.wsgi' cannot be loaded as Python module.
[Mon Jul 14 14:37:09 2014] [error] [client 70.180.246.22] mod_wsgi (pid=14292): Exception occurred processing WSGI script '/home/aaron/public_html/flapsta.com/flapsta/flapsta/flapsta.wsgi'.
[Mon Jul 14 14:37:09 2014] [error] [client 70.180.246.22] Traceback (most recent call last):
[Mon Jul 14 14:37:09 2014] [error] [client 70.180.246.22]   File "/home/aaron/public_html/flapsta.com/flapsta/flapsta/flapsta.wsgi", line 16, in <module>
[Mon Jul 14 14:37:09 2014] [error] [client 70.180.246.22]     execfile(activate_env, dict(__file__=activate_env))
[Mon Jul 14 14:37:09 2014] [error] [client 70.180.246.22]   File "/home/.virtualenvs/flapsta/bin/activate", line 4
[Mon Jul 14 14:37:09 2014] [error] [client 70.180.246.22]     deactivate () {
[Mon Jul 14 14:37:09 2014] [error] [client 70.180.246.22]                   ^
[Mon Jul 14 14:37:09 2014] [error] [client 70.180.246.22] SyntaxError: invalid syntax

I have my flapsta.wsgi file set up like so:

import os
import sys
import site

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/home/.virtualenvs/flapsta/lib/python2.7/site-packages')

# Add the app directories to the PYTHONPATH
sys.path.append('/home/aaron/public_html/flapsta.com')
sys.path.append('/home/aaron/public_html/flapsta.com/flapsta')

os.environ['DJANGO_SETTINGS_MODULE'] = 'flapsta.settings'

# Activate the virtualenv
activate_env=os.path.expanduser('/home/.virtualenvs/flapsta/bin/activate')
execfile(activate_env, dict(__file__=activate_env))

import django.core.handlers.wsgi
_application = django.core.handlers.wsgi.WSGIHandler()

def application(environ, start_response):
    # DB
    # .... envirtonment variables ....

    return _application(environ, start_response)

When I comment out this line:

execfile(activate_env, dict(__file__=activate_env))

It no longer activates the virtualenv and I can load the site fine, but I can't use virtualenv obviously to host multiple sites with different dependencies.

Is this a known issue, or am I missing a piece of configuration somewhere that someone could help point out?

I'm running:

Django 1.5.4
viritualenv 1.11.1
Apache2.2
Ubuntu 13.04
Python 2.7.4

Thanks in advance.

回答1:

You're trying to execute the bash activate script in Python. You should be executing the activate_this.py Python script.