Error Mod_python: ImportError: Failed to import settings

Trying to get Django to work with Apache, and I get the following error:

ImportError: Failed to import settings "MyDjangoApp.settings" (is it on sys.path? Does it have syntax errors?): There is no module named MyDjangoApp.settings

My Django app is located in / home / user / django / MyDjangoApp /

My httpd.conf Location section looks like this:

<Location "/MyDjangoApp/"> SetHandler python-program PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE MKSearch.settings PythonOption django.root /MyDjangoApp PythonPath "['/home/user/django/MyDjangoApp/','/var/www'] + sys.path" PythonDebug On </Location> 

Please tell me how to fix the location section for Django to work?

+6
django apache mod-python
source share
3 answers

I think mod_python is looking for settings in the MKSearch module, which does not exist in the / home / user / django / MyDjangoApp directory. Try adding the parent directory to the PythonPath directive, as shown below:

 <Location "/MyDjangoApp/"> SetHandler python-program PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE MKSearch.settings PythonOption django.root /MyDjangoApp PythonPath "['/home/user/django/', '/home/user/django/MyDjangoApp,'/var/www'] + sys.path" PythonDebug On </Location> 

Or remove the module name from env var DJANGO_SETTINGS_MODULE, as shown below:

 <Location "/MyDjangoApp/"> SetHandler python-program PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE settings PythonOption django.root /MyDjangoApp PythonPath "['/home/user/django/MyDjangoApp,'/var/www'] + sys.path" PythonDebug On </Location> 
+11
source share

Providing this answer for completeness - even if your case was different.

I once called the django test project. Well, django imported a python module test - which is a module for testing regression and has nothing to do with my project.

This error occurs if python finds another module with the same name as your django project. Name your project in a specific way or add the path of the parent directory of your application to sys.path.

+4
source share

The following example works, located in my Apache configuration file. It was very difficult for me to get mod_python to work, despite good answers from people. For the most part, the comments I received all told me to use mod_wsgi instead of mod_python.

The comments I saw, including from the Boston Python Meetup, all concur mod_wsgi are easier to use. In my case of Red Hat EL 5 WS, switching from mod_python is not practical.

 <Location /> SetHandler python-program PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE settings PythonOption django.root /home/amr/django/amr PythonPath "['/home/amr/django', '/home/amr/django/amr', '/usr/local/lib /site-packages/django'] + sys.path" PythonDebug On </Location> 
-one
source share

All Articles