我创建了一个Python Web应用程序,其目录结构如下:
# cd /usr/local/www/myapp
modules
layout
__init__.py
layout.py
packages
public
myapp.wsgi
我已将PYTHONPATH设置为:
/usr/local/www/myapp/modules:/usr/local/www/myapp/packages
在myapp.wsgi中,我试图做以下操作:
import layout
但是我遇到了内部服务器错误,为什么呢?
这是我的 myapp.wsgi 文件(如果我注释掉 import layout 这一行,它就可以正常工作):
import sys
import wsgiref
import layout
def application(environ, start_response):
response_status = '200 OK'
response_body = 'Hello! '
response_headers = []
content_type = ('Content-type', 'text-plain')
content_length = ('Content-Length', str(len(response_body)))
response_headers.append(content_type)
response_headers.append(content_length)
start_response(response_status, response_headers)
return [response_body]
我收到的完整错误信息如下:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster@example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
我的虚拟主机配置:
<VirtualHost *:80>
ServerName localhost
ServerAlias localhost
ServerAdmin webmaster@example.com
DocumentRoot /usr/local/www/myapp/public
<Directory /usr/local/www/myapp/public>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /usr/local/www/myapp/myapp.wsgi
<Directory /usr/local/www/myapp>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
/var/log/httpd-error.log 中的错误:
[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] mod_wsgi (pid=1725): Target WSGI script '/usr/local/www/myapp/myapp.wsgi' cannot be loaded as Python module.
[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] mod_wsgi (pid=1725): Exception occurred processing WSGI script '/usr/local/www/myapp/myapp.wsgi'.
[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] Traceback (most recent call last):
[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] File "/usr/local/www/myapp/myapp.wsgi", line 3, in <module>
[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] import layout
[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] ImportError: No module named layout
print sys.path的输出结果:
layout.py
包含所需的代码,则需要执行import layout.layout
或from layout import layout
。 - jrennie