Django WSGI 部署。无法导入名称 'SimpleCookie'

Django WSGI deployment. cannot import name 'SimpleCookie'

我正在尝试部署 Django 站点。它按预期在开发服务器中运行。 运行 python 在cp虚拟环境下也导入成功

但是在 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'

似乎这解决了原来的错误,但引发了一个新错误

编辑2: 我试着添加

exec(open("/var/www/.virtualenvs/cp/bin/activate_this.py").read())

在调用get_wsgi_application()之前,但这并不能解决_sqlite3导入错误

关闭此问题并为新错误打开一个新问题:

我删除了不必要的路径插入

sys.path.insert(0, '/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django')

似乎这解决了原始错误