Django WSGI deployment. cannot import name 'SimpleCookie'

1.6k views Asked by At

I am trying to deploy Django site. It runs in development server as intended. When running python under cp virtual environment import succeds as well.

But under Apache mod_wsgi I get the folloing error log for the site:

[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'

It seems that import of SimpleCookie was done correctly first time, but then it throws and error.

My wsgi.py file is:

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()

Edit: I do not fully understand why, but when removing

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

an error changes to another one:

 django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named '_sqlite3'

Seems that this solves original error but raises a new one.

Edit2: I tried to add

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

before calling get_wsgi_application(), but this does not solve _sqlite3 import error

Closing this question and open new one for new error: Django WSGI deployment. No module named '_sqlite3'

1

There are 1 answers

1
Leo On BEST ANSWER

I have removed unnecessary path insert

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

Seems that this solves original error.