I am trying to set up a Django test application for a personal project. I had previous Django experience, but that was all with Python 2.7.x. I would like to start using 3.x whenever possible, and this project seems like a good way to get started.
Backend - standard installation of Postgres 9.4, Apache 2.4, Python 3.4 in virtualenvwrapper, operating system - OSX 10.10. mod_wsgi and mod_wsgi3 were installed via homebrew .
However, there are problems getting mod_wsgi and mod_wsgi3 to work with python 3. In particular ./manage.py runserver 8080 works fine, but when I try to use the virtual host configuration with Apache, I get
mod_wsgi (pid=29906): Target WSGI script '/Users/jason/projects/geocode_django/geodjango/geodjango/wsgi.py' cannot be loaded as Python module. mod_wsgi (pid=29906): Exception occurred processing WSGI script '/Users/jason/projects/geocode_django/geodjango/geodjango/wsgi.py'. Traceback (most recent call last): File "/Users/jason/projects/geocode_django/geodjango/geodjango/wsgi.py", line 28, in <module> application = get_wsgi_application() File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application django.setup() File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/__init__.py", line 21, in setup apps.populate(settings.INSTALLED_APPS) File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate app_config.import_models(all_models) File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/apps/config.py", line 197, in import_models self.models_module = import_module(models_module_name) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/contrib/auth/models.py", line 40, in <module> class Permission(models.Model): File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/db/models/base.py", line 125, in __new__ new_class.add_to_class('_meta', Options(meta, **kwargs)) File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/db/models/base.py", line 300, in add_to_class value.contribute_to_class(cls, name) File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/db/models/options.py", line 166, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/db/__init__.py", line 40, in __getattr__ return getattr(connections[DEFAULT_DB_ALIAS], item) File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/db/utils.py", line 242, in __getitem__ backend = load_backend(db['ENGINE']) File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/db/utils.py", line 108, in load_backend return import_module('%s.base' % backend_name) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 2, in <module> from django.db.backends.postgresql_psycopg2.base import DatabaseWrapper as Psycopg2DatabaseWrapper File "/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 27, in <module> raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e) ImproperlyConfigured: Error loading psycopg2 module: dlopen(/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/ site-packages/psycopg2/_psycopg.so, 2): Symbol not found: _PyBytes_Type Referenced from: /Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/psycopg2/_psycopg.so Expected in: flat namespace in /Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages/psycopg2/_psycopg.so
My wsgi.py file
import os, sys, site site.addsitedir("/Users/jason/.virtualenvs/geodev_env3/lib/python3.4/site-packages") sys.path.append("/Users/jason/projects/geocode_django/geodjango") sys.path.append("/Users/jason/projects/geocode_django/geodjango/geodjango") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "geodjango.settings") with open("/Users/jason/.virtualenvs/geodev_env3/bin/activate_this.py") as f: code = compile(f.read(), "/Users/jason/.virtualenvs/geodev_env3/bin/activate_this.py", "exec") exec(code, dict(__file__="/Users/jason/.virtualenvs/geodev_env3/bin/activate_this.py")) from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
and virtual host file:
LoadModule wsgi_module /usr/local/Cellar/mod_wsgi3/3.5/libexec/mod_wsgi.so <VirtualHost *:80> ServerName dev.geocode.com ServerAlias geocode.com ServerAdmin jason@someschool.edu DocumentRoot "/Users/jason/projects/geocode_django" Alias /static/ /Users/jason/projects/geocode_django/static/ WSGIScriptAlias / /Users/jason/projects/geocode_django/geodjango/geodjango/wsgi.py <Directory /Users/jason/projects/geocode_django/static > require all granted </Directory> <Directory /Users/jason/projects/geocode_django/geodjango/geodjango > <Files wsgi.py > Require all granted </Files> </Directory> CustomLog /Users/jason/projects/geocode_django/logs/access.log combined ErrorLog /Users/jason/projects/geocode_django/logs/error.log </VirtualHost>
My problem is that if I have python 2.7 virtualenv with the same virtual host with LoadModule pointing to
LoadModule wsgi_module /usr/local/Cellar/mod_wsgi/4.4.7/libexec/mod_wsgi.so
and replace
with open("/Users/jason/.virtualenvs/geodev_env3/bin/activate_this.py") as f: code = compile(f.read(), "/Users/jason/.virtualenvs/geodev_env3/bin/activate_this.py", "exec") exec(code, dict(__file__="/Users/jason/.virtualenvs/geodev_env3/bin/activate_this.py"))
in wsgi.py with
exec(open("/Users/jasonjohns/.virtualenvs/geocode_env/bin/activate_this.py").read())
I can download the site from the URL dev.geocode.com . Otherwise, I get a 500 error page with an error in the log file.
I initially thought this was a problem with psycopg2, and sent an error report . However, the developer closed the problem, as far as mod_wsgi is concerned, not psycopg.
Other than compiling mod_wsgi for my local environment, is there any way to fix this problem?