apache Mod_python 错误:ImportError:无法导入设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/962533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mod_python error: ImportError: Could not import settings
提问by R0b0tn1k
Trying to get Django to work with Apache, and I'm getting the following error:
试图让 Django 与 Apache 一起工作,我收到以下错误:
ImportError: Could not import settings 'MyDjangoApp.settings' (Is it on sys.path? Does it have syntax errors?): No module named MyDjangoApp.settings
导入错误:无法导入设置“MyDjangoApp.settings”(是否在 sys.path 上?是否有语法错误?):没有名为 MyDjangoApp.settings 的模块
My Django app is located in /home/user/django/MyDjangoApp/
我的 Django 应用程序位于 /home/user/django/MyDjangoApp/
My httpd.conf Location section looks like:
我的 httpd.conf 位置部分如下所示:
<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 correct the location section to make Django work?
请告诉我如何更正位置部分以使 Django 工作?
回答by sleepyjames
I think mod_python is looking for settings in the MKSearch module which doesn't exist in side the /home/user/django/MyDjangoApp directory. Try adding the parent dir to the PythonPath directive as below:
我认为 mod_python 正在寻找 /home/user/django/MyDjangoApp 目录中不存在的 MKSearch 模块中的设置。尝试将父目录添加到 PythonPath 指令中,如下所示:
<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 the DJANGO_SETTINGS_MODULE env var as below:
或者从 DJANGO_SETTINGS_MODULE 环境变量中删除模块名称,如下所示:
<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>
回答by Evgeny
Giving this answer for completeness - even though your case was different.
给出这个答案是为了完整性 - 即使你的情况不同。
I've once named my django project test. Well, django was importing python module test - which is a module for regression testing and has nothing to do with my project.
我曾经将我的 django 项目命名为test。好吧,django 正在导入 python 模块 test - 这是一个用于回归测试的模块,与我的项目无关。
This error will occur if python finds another module with the same name as your django project. Name your project in a distinct way, or prepend the path of the parent directory of your application to sys.path.
如果 python 发现另一个与您的 django 项目同名的模块,则会发生此错误。以独特的方式命名您的项目,或将应用程序的父目录的路径添加到 sys.path。
回答by octopusgrabbus
The following example, located in my Apache configuration file, works. I found it very hard to get mod_python to work despite good answers from folks. For the most part, the comments I received all said to use mod_wsgi, instead of mod_python.
位于我的 Apache 配置文件中的以下示例有效。尽管人们给出了很好的答案,但我发现很难让 mod_python 工作。在大多数情况下,我收到的评论都说使用 mod_wsgi,而不是 mod_python。
Comments I've seen including from the Boston Python Meetup all concur mod_wsgi is easier to use. In my case on Red Hat EL 5 WS, changing from mod_python is not practical.
我看到的评论包括来自波士顿 Python 聚会的所有评论都同意 mod_wsgi 更易于使用。就我在 Red Hat EL 5 WS 上的情况而言,从 mod_python 更改是不切实际的。
<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>

