0
votes

I made a very simple project with django using python 3.4, django 1.10.2, and virtualenv all on FreeBSD. I cannot get mod_wsgi to work for the life of me, and I have done almost nothing beyond building the project and running manage.py migrate. It seems to be having a problem importing sqlite3 but in the virtualenv I can run python and import sqlite3 and _sqlite3.

I get the following:

mod_wsgi (pid=15765): Target WSGI script '/server/apache/partner/partner/wsgi.py' cannot be loaded as Python module.
 mod_wsgi (pid=15765): Exception occurred processing WSGI script '/server/apache/partner/partner/wsgi.py'.
 Traceback (most recent call last):
 File "/server/apache/partner/partner/wsgi.py", line 20, in <module>
     application = get_wsgi_application()
 File "/server/apache/partner-env/lib/python3.4/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
     django.setup(set_prefix=False)
 File "/server/apache/partner-env/lib/python3.4/site-packages/django/__init__.py", line 27, in setup
     apps.populate(settings.INSTALLED_APPS)
 File "/server/apache/partner-env/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
     app_config.import_models(all_models)
 File "/server/apache/partner-env/lib/python3.4/site-packages/django/apps/config.py", line 199, in import_models
     self.models_module = import_module(models_module_name)
 File "/usr/local/lib/python2.7/importlib/__init__.py", line 37, in import_module
     __import__(name)
 File "/server/apache/partner-env/lib/python3.4/site-packages/django/contrib/auth/models.py", line 4, in <module>
     from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
 File "/server/apache/partner-env/lib/python3.4/site-packages/django/contrib/auth/base_user.py", line 52, in <module>
     class AbstractBaseUser(models.Model):
 File "/server/apache/partner-env/lib/python3.4/site-packages/django/db/models/base.py", line 119, in __new__
     new_class.add_to_class('_meta', Options(meta, app_label))
 File "/server/apache/partner-env/lib/python3.4/site-packages/django/db/models/base.py", line 316, in add_to_class
     value.contribute_to_class(cls, name)
 File "/server/apache/partner-env/lib/python3.4/site-packages/django/db/models/options.py", line 214, in contribute_to_class
     self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
 File "/server/apache/partner-env/lib/python3.4/site-packages/django/db/__init__.py", line 33, in __getattr__
     return getattr(connections[DEFAULT_DB_ALIAS], item)
 File "/server/apache/partner-env/lib/python3.4/site-packages/django/db/utils.py", line 211, in __getitem__
     backend = load_backend(db['ENGINE'])
 File "/server/apache/partner-env/lib/python3.4/site-packages/django/db/utils.py", line 115, in load_backend
     return import_module('%s.base' % backend_name)
 File "/usr/local/lib/python2.7/importlib/__init__.py", line 37, in import_module
     __import__(name)
 File "/server/apache/partner-env/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 39, in <module>
     raise ImproperlyConfigured("Error loading either pysqlite2 or sqlite3 modules (tried in that order): %s" % exc)
 ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named _sqlite3

wsgi.py:

import os
from sys import path
from django.core.wsgi import get_wsgi_application

add_path = '/server/apache/partner'
if add_path not in path:
    path.append(add_path)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "partner.settings")

application = get_wsgi_application()

-gns

1
Have you set WSGIPythonHome? - Antonis Christofides
It is more likely that he has set WSGIPythonHome or WSGIPythonPath to try and force mod_wsgi to use a different Python version/installation/virtual environment than it was compiled for. You cannot do this. See how the messages mention both Python 2,7 and 3.4. You must recompile mod_wsgi specifically for Python 3.4. You can't try and force it to use a Python 3.4 installation when it is compiled for Python 2.7. - Graham Dumpleton
Graham, that is exactly what I did. Thanks! - Greg Schmit
Graham, I am going to make your comment an answer but feel free to answer and I will mark yours as the solution. - Greg Schmit

1 Answers

0
votes

Thanks to @Graham Dumpleton for helping me in the comments. I had compiled mod_wsgi while python 2.7 was installed and I thought the virtualenv was enough for python 3.4, but I needed to remove python 2 and recompile mod_wsgi.

-gns