在Django中使用VirtualEnv + mod-wsgi的SyntaxError

时间:2014-07-14 15:04:33

标签: python django virtualenv mod-wsgi wsgi

我在使用VirtualEnv的生产Ubuntu 13.04服务器上使用mod-wsgi时遇到问题。

有人能够指出问题可能是什么吗?

以下是我收到以下语法错误的回溯:

[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

我的flapsta.wsgi文件设置如下:

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)

当我注释掉这一行时:

execfile(activate_env, dict(__file__=activate_env))

它不再激活virtualenv,我可以正常加载网站,但我无法使用virtualenv来托管具有不同依赖关系的多个网站。

这是一个已知问题,还是我错过了某人可以帮助指出的某个配置?

我正在跑步:

Django 1.5.4
viritualenv 1.11.1
Apache2.2
Ubuntu 13.04
Python 2.7.4

提前致谢。

1 个答案:

答案 0 :(得分:3)

您正在尝试在Python中执行bash激活脚本。您应该执行activate_this.py Python脚本。