目标WSGI脚本'../wsgi.py'无法作为Python模块加载 [英] Target WSGI script '../wsgi.py' cannot be loaded as Python module

查看:119
本文介绍了目标WSGI脚本'../wsgi.py'无法作为Python模块加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在部署django项目并收到500错误(请参阅服务器日志错误).

I'm deploying a django project and getting that 500 error (see Server log error).

我在哪里做错了?

一些注意事项:

  • centOS
  • 已安装mod_wsgi
  • 在开发和部署环境中,
  • 相同的python(2.7)和django(1.9.6)版本
  • 在部署环境中使用virtualenv
  • centOS
  • mod_wsgi installed
  • same python (2.7) and django (1.9.6) version in both develop and deploy environement
  • using virtualenv in deploy environement

服务器日志错误

[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]  SyntaxError: invalid syntax
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] mod_wsgi (pid=6570): Target WSGI script '/new_esmart/esmart2/esmart2/wsgi.py' cannot be loaded as Python module., referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] mod_wsgi (pid=6570): Exception occurred processing WSGI script '/new_esmart/esmart2/esmart2/wsgi.py'., referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] Traceback (most recent call last):, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]   File "/new_esmart/esmart2/esmart2/wsgi.py", line 13, in <module>, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]     import django.core.handlers.wsgi, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]   File "/new_esmart/esmart_env/lib/python2.7/site-packages/django/__init__.py", line 1, in <module>, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]     from django.utils.version import get_version, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]   File "/new_esmart/esmart_env/lib/python2.7/site-packages/django/utils/version.py", line 7, in <module>, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]     from django.utils.lru_cache import lru_cache, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]   File "/new_esmart/esmart_env/lib/python2.7/site-packages/django/utils/lru_cache.py", line 28, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]      fasttypes = {int, str, frozenset, type(None)},, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]                      ^, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]  SyntaxError: invalid syntax, referer: http://192.168.30.17/logistics/alarms/

wsgi.py

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

# Add the app's directory to the PYTHONPATH
sys.path.append('/new_esmart/esmart2')
sys.path.append('/new_esmart/esmart2/esmart2')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "esmart2.settings")

# Activate your virtual env
activate_env = os.path.expanduser("/new_esmart/esmart_env/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))

application = django.core.handlers.wsgi.WSGIHandler()

httpd.conf

<VirtualHost *:80>
    ServerAdmin blahblah@blah.it
    DocumentRoot /new_esmart/esmart2
    ServerName logistica.org
    ServerAlias www.logistica.org
    WSGIScriptAlias / /new_esmart/esmart2/esmart2/wsgi.py
    ErrorLog logs/logistica.org-error_log
    CustomLog logs/logistica.org-access_log common
</VirtualHost>

WSGIPythonPath /new_esmart/esmart2:/new_esmart/esmart_env/lib/python2.7/site-packages

/etc/httpd/conf.d/wsgi.conf

LoadModule wsgi_module modules/mod_wsgi.so

推荐答案

正如Daniel指出的那样,该错误表明您的mod_wsgi是针对Python 2.6编译的.为了使用Python 2.7,您需要安装针对Python 2.7的mod_wsgi.您不能仅仅通过引用您的Python 2.7虚拟环境来试图强迫它使用Python 2.7,这不是它的工作原理.

As pointed out by Daniel, the error indicates your mod_wsgi is compiled for Python 2.6. In order to use Python 2.7 you will need to have mod_wsgi installed with it compiled against Python 2.7. You cannot try and force it to use Python 2.7 by simply referring to your Python 2.7 virtual environment, that isn't how it works.

您可以使用以下描述的测试应用程序来验证使用哪个Python版本mod_wsgi进行编译:

You can verify what Python version mod_wsgi was compiled with using test application described in:

您需要卸载mod_wsgi模块并安装为Python 2.7构建的模块版本,或者从系统软件包中安装(如果有),或者从源代码构建(如果没有适用于mod_wsgi for Python 2.7的系统软件包).

You will need to uninstall mod_wsgi module and install a version of it built for Python 2.7, either from system packages if available, or built from source code if no system packages available for mod_wsgi for Python 2.7.

我还建议您检查有关使用mod_wsgi的Django文档,并确保按照说明使用守护程序模式.

I would also recommend that you check the Django documentation on using mod_wsgi and ensure that you use daemon mode as it explains.

请注意,Django文档仍未遵循所有最佳实践.您应该使用python-home引用虚拟环境,而不是在python-path中显式添加site-packages.参见:

Just be aware that the Django documentation still doesn't follow all best practices. Rather than add the site-packages explicitly in python-path, you should use python-home to refer to the virtual environment. See:

这篇关于目标WSGI脚本'../wsgi.py'无法作为Python模块加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆