0

I am trying to use Django for my next web project, and (thought) I had everything installed nicely, but I try to visit my site (a minimal new django project) and I get a 500 error.

My set up uses apache, mod_wsgi, and I'm using my mac for development.

Looking in the apache error logs, I see

ImportError: Could not import settings 'mysite.settings' 
(Is it on sys.path? Is there an import error in the settings file?): 
No module named mysite.settings

First, I double check which python mod_wsgi is using:

wpa072077:mysite enewe101$ otool -L /usr/local/Cellar/mod_wsgi/3.3/libexec/mod_wsgi.so
/usr/local/Cellar/mod_wsgi/3.3/libexec/mod_wsgi.so:
    /Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
    /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 635.21.0)

Ok, so i fire up that python and try importing mysite.settings -- It works with no problem, because I put it on my PYTHONPATH.

Hmm, maybe mod_wsgi isn't getting my PYTHONPATH? So I try adding this to my http.conf inside the Virtual Host section for my site:

SetEnv PYTHONPATH /Users/enewe101/projects/mysite/src

(The mysite module is at /Users/enewe101/projects/mysite/src/mysite and it contains __init__.py as it should)

One last thing: I have two versions of python on my machine. As I mentioned, I checked which one mod_wsgi was compiled against and if I enter the REPL for that one I can import mysite.settings no problem. However, I actually wish mod_wsgi to use a different python -- but same version (both 2.7). I try forcing mod_wsgi to use my desired python by putting this in httpd.conf:

WSGIPythonHome /Library/Frameworks/EPD64.framework/Versions/7.3
WSGIPythonPath /Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages:/Library/Python/2.7/site-packages:/Users/enewe101/projects/mysite/src

but it causes the server to simply not respond -- my browser spins until I get impatient.

How can I resolve this import error, and preferably get mod_wsgi to use the correct python?

Related:

Community
  • 1
  • 1
Edward Newell
  • 17,203
  • 7
  • 34
  • 36
  • See http://stackoverflow.com/a/23248187/2428955 maybe your wsgi.py needs an explicit path to your project directly in it. – Ambroise May 20 '14 at 17:53
  • @Ambroise Thanks. Sure, that works, but that means I have to explicitly add paths for all the modules I might want, and this adds machine-specific config to my wsgi.py which shouldn't be necessary. – Edward Newell May 20 '14 at 20:09

1 Answers1

1

If the mod_wsgi.so is finding the wrong library when checking with tool -L, you need to recompile it from source code again doing:

make distclean
./configure --disable-framework --with-python=/Library/Frameworks/EPD64.framework/Versions/7.3/bin/python
make
sudo make install

The key is the '--disable-framework'. Some Python distros are broken and don't work properly when trying to link them as a MacOS X style framework rather that library. That option forces it to use UNIX style -L/-l library linking.

Make sure you then do not set either WSGIPythonHome or WSGIPythonPath.

Using SetEnv also will not do anything as it doesn't set process level environment variables.

With those changes, then go back and deploy Django by following the mod_wsgi instructions in the Django docs site.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134