Django WSGI deployment. No module named '_sqlite3'

571 views Asked by At

I try to deploy Django application under Django 1.8 and Apache mod_wsgi (python 3.4.3, ubuntu 14.04)

I have solved the previous error with import ( Django WSGI deployment. cannot import name 'SimpleCookie' ) but then I meet another one:

Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named '_sqlite3'

Here bigger piece of the error log:

[Wed Jun 10 18:33:44.021939 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "<frozen importlib._bootstrap>", line 1448, in exec_module
[Wed Jun 10 18:33:44.021960 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
[Wed Jun 10 18:33:44.022198 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django/contrib/auth/models.py", line 41, in <module>
[Wed Jun 10 18:33:44.022207 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]     class Permission(models.Model):
[Wed Jun 10 18:33:44.022760 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django/db/models/base.py", line 139, in __new__
[Wed Jun 10 18:33:44.022770 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]     new_class.add_to_class('_meta', Options(meta, **kwargs))
[Wed Jun 10 18:33:44.022807 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django/db/models/base.py", line 324, in add_to_class
[Wed Jun 10 18:33:44.022814 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]     value.contribute_to_class(cls, name)
[Wed Jun 10 18:33:44.023151 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django/db/models/options.py", line 250, in contribute_to_class
[Wed Jun 10 18:33:44.023161 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]     self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
[Wed Jun 10 18:33:44.023296 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django/db/__init__.py", line 36, in __getattr__
[Wed Jun 10 18:33:44.023304 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]     return getattr(connections[DEFAULT_DB_ALIAS], item)
[Wed Jun 10 18:33:44.023514 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django/db/utils.py", line 240, in __getitem__
[Wed Jun 10 18:33:44.023558 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]     backend = load_backend(db['ENGINE'])
[Wed Jun 10 18:33:44.023603 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django/db/utils.py", line 111, in load_backend
[Wed Jun 10 18:33:44.023610 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]     return import_module('%s.base' % backend_name)
[Wed Jun 10 18:33:44.023642 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "/usr/local/lib/python3.4/importlib/__init__.py", line 109, in import_module
[Wed Jun 10 18:33:44.023649 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]     return _bootstrap._gcd_import(name[level:], package, level)
[Wed Jun 10 18:33:44.023669 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
[Wed Jun 10 18:33:44.023690 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
[Wed Jun 10 18:33:44.023711 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
[Wed Jun 10 18:33:44.023732 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
[Wed Jun 10 18:33:44.023752 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "<frozen importlib._bootstrap>", line 1129, in _exec
[Wed Jun 10 18:33:44.023773 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "<frozen importlib._bootstrap>", line 1448, in exec_module
[Wed Jun 10 18:33:44.023793 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
[Wed Jun 10 18:33:44.023826 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]   File "/var/www/.virtualenvs/cp/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 36, in <module>
[Wed Jun 10 18:33:44.023833 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780]     raise ImproperlyConfigured("Error loading either pysqlite2 or sqlite3 modules (tried in that order): %s" % exc)
[Wed Jun 10 18:33:44.023862 2015] [:error] [pid 19282:tid 140071189554944] [client 192.168.1.1:16780] django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named '_sqlite3'

When running development server no errors appear. There is no errors as well when running in python interpreter in virtualenv cp

>>>import sqlite3

My wsgi.py file is:

import os, sys, site

sys.path.insert(0,'/var/www/.virtualenvs/cp/lib/python3.4/site-packages')
sys.path.insert(0,'/var/www/django_projects/cp')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

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

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Here is Apache's virtualhost's conf for the site:

<VirtualHost *:80>
    ServerName cp.mysite.com
    WSGIDaemonProcess cp user=www-data group=www-data threads=5
    WSGIApplicationGroup cp
    WSGIScriptAlias / /var/www/django_projects/cp/mysite/wsgi.py

    <Directory /var/www/django_projects/cp/mysite>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    ErrorLog    /var/www/errlogs/cperror.log
</VirtualHost>
1

There are 1 answers

0
Leo On BEST ANSWER

Finaly, after making a debug with a help of simple wsgi.py (thanks to @GrahamDumpleton for the link), I've found that my resulting sys.path is not too clean.

So I have manually changed sys.path in wsgi.py taking it from python started in virtualenv cp and then added project directory to this list at the begining in wsgi.py:

import os, sys

virtual_env = '/var/www/.virtualenvs/cp'    
sys.path=['%s/lib/python34.zip' % (virtual_env,), 
          '%s/lib/python3.4' % (virtual_env,), 
          '%s/lib/python3.4/plat-linux' % (virtual_env,), 
          '%s/lib/python3.4/lib-dynload' % (virtual_env,), 
          '%s/lib/python3.4/site-packages' % (virtual_env,),
          #'/var/www/python/3.4.3/lib/python3.4', 

          ]
sys.path.insert(0,'/var/www/django_projects/cp')

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Possibly this is not the "best practice" solution, but it does remove magic functions and easy to understand and repeat for new user like me :)