我正在尝试部署Django网站。它按预期在开发服务器中运行。 在cp虚拟环境下运行python时也可以导入成功。
但是在Apache mod_wsgi下我得到了该站点的以下错误日志:
[Wed Jun 10 17:26:20.204238 2015] [:info] [pid 18376:tid 140307600816000] mod_wsgi (pid=18376): Attach interpreter ''.
[Wed Jun 10 17:26:47.476066 2015] [:info] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] mod_wsgi (pid=18377, process='', application='cp'): Loading WSGI script '/var/www/django_projects/cp/mysite/wsgi.py'.
[Wed Jun 10 17:26:47.575882 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] mod_wsgi (pid=18377): Target WSGI script '/var/www/django_projects/cp/mysite/wsgi.py' cannot be loaded as Python module.
[Wed Jun 10 17:26:47.575951 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] mod_wsgi (pid=18377): Exception occurred processing WSGI script '/var/www/django_projects/cp/mysite/wsgi.py'.
[Wed Jun 10 17:26:47.575996 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] Traceback (most recent call last):
[Wed Jun 10 17:26:47.576201 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] File "/var/www/django_projects/cp/mysite/wsgi.py", line 23, in <module>
[Wed Jun 10 17:26:47.576209 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] from django.core.wsgi import get_wsgi_application
[Wed Jun 10 17:26:47.576319 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] File "/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django/core/wsgi.py", line 2, in <module>
[Wed Jun 10 17:26:47.576327 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] from django.core.handlers.wsgi import WSGIHandler
[Wed Jun 10 17:26:47.576485 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] File "/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django/core/handlers/wsgi.py", line 11, in <module>
[Wed Jun 10 17:26:47.576493 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] from django import http
[Wed Jun 10 17:26:47.576600 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] File "/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django/http/__init__.py", line 1, in <module>
[Wed Jun 10 17:26:47.576608 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] from django.http.cookie import SimpleCookie, parse_cookie
[Wed Jun 10 17:26:47.576738 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] File "/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django/http/cookie.py", line 7, in <module>
[Wed Jun 10 17:26:47.576746 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] from django.utils.six.moves import http_cookies
[Wed Jun 10 17:26:47.577075 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] File "/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django/utils/six.py", line 90, in __get__
[Wed Jun 10 17:26:47.577084 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] result = self._resolve()
[Wed Jun 10 17:26:47.577119 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] File "/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django/utils/six.py", line 113, in _resolve
[Wed Jun 10 17:26:47.577126 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] return _import_module(self.mod)
[Wed Jun 10 17:26:47.577156 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] File "/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django/utils/six.py", line 80, in _import_module
[Wed Jun 10 17:26:47.577163 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] __import__(name)
[Wed Jun 10 17:26:47.577193 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] File "/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django/http/__init__.py", line 1, in <module>
[Wed Jun 10 17:26:47.577200 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] from django.http.cookie import SimpleCookie, parse_cookie
[Wed Jun 10 17:26:47.577228 2015] [:error] [pid 18377:tid 140307470911232] [client 192.168.1.1:15841] ImportError: cannot import name 'SimpleCookie'
似乎第一次正确完成了SimpleCookie的导入,但随后它抛出并出错。
我的wsgi.py文件是:
import os
import sys
import site
sys.path.insert(0,'/var/www/.virtualenvs/cp/lib/python3.4/site-packages')
sys.path.insert(0, '/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django')
# Add the app's directory to the PYTHONPATH
sys.path.insert(0,'/var/www/django_projects/cp')
#sys.path.insert(0,'/var/www/django_projects/cp/mysite')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
修改 我不完全理解为什么,但在删除时
sys.path.insert(0, '/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django')
错误更改为另一个错误:
django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named '_sqlite3'
似乎这解决了原始错误但提出了一个新错误。
EDIT2: 我试着添加
exec(open("/var/www/.virtualenvs/cp/bin/activate_this.py").read())
调用 get_wsgi_application()之前,但这不解决_sqlite3导入错误
关闭此问题并为新错误打开新问题: Django WSGI deployment. No module named '_sqlite3'
答案 0 :(得分:9)
我删除了不必要的路径插入
sys.path.insert(0, '/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django')
似乎这解决了原始错误。