本文介绍了Django newbie部署问题 - ImportError:无法导入设置'设置'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该应用运行正常使用django内部服务器,但是当我使用apache + mod_python我得到以下错误

The app runs fine using django internal server however when I use apache + mod_python I get the below error

  File "/usr/local/lib/python2.6/dist-packages/django/conf/__init__.py", line 75, in __init__
    raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)

ImportError: Could not import settings 'settings' (Is it on sys.path? Does it have syntax errors?): No module named settings






这是需要的信息


Here is the needed information

1)项目目录:/ root / djangoprojects / mysite

1) Project directory: /root/djangoprojects/mysite

2)/ root / djangoprojects / mysite的目录列表

2) directory listing of /root/djangoprojects/mysite

ls -ltr
total 28
-rw-r--r-- 1 root root  546 Aug  1 08:34 manage.py
-rw-r--r-- 1 root root    0 Aug  1 08:34 __init__.py
-rw-r--r-- 1 root root  136 Aug  1 08:35 __init__.pyc
-rw-r--r-- 1 root root 2773 Aug  1 08:39 settings.py
-rw-r--r-- 1 root root 1660 Aug  1 08:53 settings.pyc
drwxr-xr-x 2 root root 4096 Aug  1 09:04 polls
-rw-r--r-- 1 root root  581 Aug  1 10:06 urls.py
-rw-r--r-- 1 root root  314 Aug  1 10:07 urls.pyc

3)应用程序目录:/ root / djangoprojects / mysite / polls

3) App directory : /root/djangoprojects/mysite/polls

4)/ root / djangoprojects / mysite / polls目录列表

4) directory listing of /root/djangoprojects/mysite/polls

ls -ltr
total 20
-rw-r--r-- 1 root root 514 Aug  1 08:53 tests.py
-rw-r--r-- 1 root root  57 Aug  1 08:53 models.py
-rw-r--r-- 1 root root   0 Aug  1 08:53 __init__.py
-rw-r--r-- 1 root root 128 Aug  1 09:02 views.py
-rw-r--r-- 1 root root 375 Aug  1 09:04 views.pyc
-rw-r--r-- 1 root root 132 Aug  1 09:04 __init__.pyc

5)文件系统中的任何地方运行导入django在python解释器工作正常

5) Anywhere in the filesystem running import django in python interpreter works fine

6)httpd.conf的内容

6) content of httpd.conf

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

7)PYTHONPATH变量设置为

7) PYTHONPATH variable is set to

echo $PYTHONPATH
/root/djangoprojects/mysite

8)DJANGO_SETTINGS_MODULE设置为

8) DJANGO_SETTINGS_MODULE is set to

echo $DJANGO_SETTINGS_MODULE
mysite.settings

9)sys.path的内容是

9) content of sys.path is

import sys
>>> sys.path
['', '/root/djangoprojects/mysite', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/local/lib/python2.6/dist-packages']

如何将设置位置添加到sys.path,使其在会话之间持续存在?

How do I add settings location to sys.path such that it persistent across sessions ?

我已经看到没有帖子与人有相同的问题,我已经尝试了很多完全击败我,我需要做的。

I have read umpteen no of post with people having the same issue it and I have tried a lot completely beats me as to what I need to do.

寻找一些帮助。

提前感谢
Ankur Gupta

Thanks in advanceAnkur Gupta

推荐答案

您的apache配置应如下所示:

Your apache configuration should look like this:

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

请注意,唯一的区别是mysite.settings。一旦配置更改(apache2ctl restart),不要忘记重新启动apache。请参阅了解更多信息。

Note that the sole difference is the "mysite.settings". Don't forget to restart apache once the config has changed (apache2ctl restart). See http://docs.djangoproject.com/en/dev/howto/deployment/modpython/ for more info.

这篇关于Django newbie部署问题 - ImportError:无法导入设置'设置'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 21:09