如何排除故障 - ImportError:无法导入设置“mysite.settings”什么时候部署django?
我已经根据 apache 部署说明部署了 django 应用程序,并且与 apache 和 mod_python 完美配合。但由于我稍微改变了项目结构,我一直无法让它工作。无论我尝试什么,我都会收到以下页面:
MOD_PYTHON ERROR
ProcessId: 27841
Interpreter: '127.0.0.1'
ServerName: '127.0.0.1'
DocumentRoot: '/var/www'
URI: '/myapp/'
Location: '/myapp/'
Directory: None
Filename: '/var/www/myapp'
PathInfo: '/'
Phase: 'PythonHandler'
Handler: 'django.core.handlers.modpython'
Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1537, in HandlerDispatch
default=default_handler, arg=req, silent=hlist.silent)
File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1229, in _process_target
result = _execute_target(config, req, object, arg)
File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1128, in _execute_target
result = object(arg)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/core/handlers/modpython.py", line 212, in handler
return ModPythonHandler()(req)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/core/handlers/modpython.py", line 174, in __call__
self.load_middleware()
File "/usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/core/handlers/base.py", line 39, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "/usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/utils/functional.py", line 276, in __getattr__
self._setup()
File "/usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/conf/__init__.py", line 89, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'settings' (Is it on sys.path?): No module named settings
读取各种线程,问题可能是以下错误之一
- apache 配置文件
- 不正确 sys.path
- 对任何文件或目录的权限不正确
- 我的 django 站点代码中的错误
有什么办法把问题缩小一些?例如,有没有办法找出错误时 sys.path 是什么?是否有任何日志文件可以帮助我?有没有办法区分“找不到文件”和“无权读取文件”?我想更科学地找到解决方案,而不是只是尝试随机的事情,直到我偶然发现解决方案。
这个问题与: Mod_python error: ImportError: Could不导入设置。我特别询问的是帮助缩小可能解决方案范围的方法,而不是解决方案本身。
I've had my django app deployed and working perfectly with apache and mod_python, according to the apache deployment instructions. But since I changed the project structure somewhat, I haven't been able to get it working. No matter what I try, I keep getting the following page:
MOD_PYTHON ERROR
ProcessId: 27841
Interpreter: '127.0.0.1'
ServerName: '127.0.0.1'
DocumentRoot: '/var/www'
URI: '/myapp/'
Location: '/myapp/'
Directory: None
Filename: '/var/www/myapp'
PathInfo: '/'
Phase: 'PythonHandler'
Handler: 'django.core.handlers.modpython'
Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1537, in HandlerDispatch
default=default_handler, arg=req, silent=hlist.silent)
File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1229, in _process_target
result = _execute_target(config, req, object, arg)
File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1128, in _execute_target
result = object(arg)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/core/handlers/modpython.py", line 212, in handler
return ModPythonHandler()(req)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/core/handlers/modpython.py", line 174, in __call__
self.load_middleware()
File "/usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/core/handlers/base.py", line 39, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "/usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/utils/functional.py", line 276, in __getattr__
self._setup()
File "/usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg/django/conf/__init__.py", line 89, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'settings' (Is it on sys.path?): No module named settings
Reading various threads, the problem could be one of the following
- incorrect apache configuration file
- incorrect sys.path
- incorrect permissions on any file or directory
- bugs in my django site code
Is there any way to narrow the problem down some? For example, is there a way to find out what sys.path is at the time of error? Are there any log files that can help me here? Is there any way to distinguish between "file not found" and "no permission to read file"? I'd like to approach the solution more scientifically than just trying random things until I stumble upon the solution.
This question is not the same as: Mod_python error: ImportError: Could not import settings. I'm asking specifically for ways to help narrow down the possible solutions, rather than the solutions themselves.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我自己解决了这个问题,所以我会回答我自己的问题。
我发现没有 apache 日志或任何其他信息源来找出到底是什么导致了导入错误。因此,我在下面分享我的方法,作为一种更“理性”的方法,希望我可以为某人节省几个令人沮丧的随机更改内容的时间。
作为记录,这是我使用的 apache 配置。
我在服务器上创建了一个用户“mysite”,django settings.py 位于(有点冗余命名)位置
/home/mysite/prg/mysite/mysite_django/settings.py
。我打开一个到服务器的 ssh 通道。在本例中,我使用非常有用的
screen
实用程序在用户之间快速切换(但第二个 ssh 通道也可以工作)按 Ctrl-A、Ctrl-C 创建一个新屏幕。
现在重要的是要像 apache 一样查看权限,因此我切换到运行 apache 的用户,即 debian 系统上的 www-data:
我将尝试自己从 python 环境导入 settings.py:
这显然应该给你标准的 sys.path ,没有任何“mysite”目录,因为我们运行的是普通的 python。所以我修改了sys.path如下。我直接从 apache 配置复制粘贴,以避免拼写错误:
正如预期的那样,我得到了与在浏览器中加载站点时看到的相同错误。这样我就可以在 python 环境中复制问题,这是一件好事。显然,构建路径应由系统路径条目“/home/mysite/prg/mysite”加上请求的导入路径“mysite_django/settings.py”组成。那么为什么不导入呢?让我们确保给定的系统路径条目确实存在
它不存在?奇怪的。
这里我意识到mysite_django的权限有问题。可能是我所做的所有随机更改的剩余部分。但实际上,问题出在上面的目录 mysite 中,因为它试图从中读取目录列表。我使用 Ctrl-A、Ctrl-N 切换到超级用户屏幕,然后使用 chmod 设置 mysite 的权限,使其与最后成功的目录相同:
切换回 www -使用 Ctrl-A、Ctrl-N 的数据屏幕:
现在进行最终测试
耶!成功
我们甚至可以进一步测试 django 是否可以加载
此时该网站在我的浏览器中再次工作。
通过执行这些步骤,您至少可以确保:
Ok, I figured out the problem myself, so I'll answer my own question.
I find that there is no apache log or any other source of information to figure out what exactly is causing the Import Error. So I share my method below, as a more "rational" approach, in the hope that I can save somebody a few frustrating hours of randomly changing stuff.
For the record, here is the apache configuration that I used.
I created a user "mysite" on the server, and the django settings.py is located in the (somewhat redundantly named) location
/home/mysite/prg/mysite/mysite_django/settings.py
.I open an ssh channel to the server. In this case I use the very useful
screen
utility to switch quickly between users (but a second ssh channel would work just as well)Press Ctrl-A, Ctrl-C to create a new screen.
Now it's important to see the permissions exactly as apache does, so I switch to the user that runs apache, which is www-data on debian systems:
I'm going to try to import settings.py myself from the python environment:
This obviously should give you the standard sys.path without any of the "mysite" directories, because we're running plain python. So I modified sys.path as follows. I copy-pasted directly from the apache configuration, to avoid typos:
As expected, I get the same error I already saw when loading my site in a browser. So I can replicate the problem within the python environment, that is a good thing. Clearly, the build path should be formed from the sys path entry '/home/mysite/prg/mysite' plus the requested import path 'mysite_django/settings.py'. So why is it not importing? Let's make sure the given sys path entry actually exists
It doesn't exist? Strange.
Here I realize that there is something wrong with the permissions of mysite_django. Probably a left-over from all the random changes that I made. But actually, the problem is in the directory above, mysite, because that is where it tries to read the directory listing from. I switch to the superuser screen with Ctrl-A, Ctrl-N, and I use
chmod
to set the permissions of mysite so that they are the same as the last directory that succeeded:Switching back to the www-data screen with Ctrl-A, Ctrl-N:
Now for the final test
Yay! Success
We can even go further and test that django can load
At this point the site was working again in my browser.
By following these steps, you can at least ensure that:
对于我的 Django 1.5 和 mod_wsgi,我这样解决:
httpd.conf:
wsgi.py:
sys.path.append(path) 让 mysite.settings 了解
另一件事,为了避免如下错误,
您需要从 httpd.conf 中删除 cgi 配置
for my Django 1.5 and mod_wsgi, I resolved it like this:
httpd.conf:
wsgi.py:
the sys.path.append(path) to let mysite.settings known
another thing, to avoid error like below
you need remove cgi configure from httpd.conf