我创建了这个目录结构的蟒蛇网络应用:无法导入模块
# 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 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, [email protected] 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 [email protected]
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
输出打印的sys.path的:
至少包含您正在收到的完整且准确的错误消息。 – unwind
你如何运行网络服务器?这很可能不会使用您的环境,因此为您自己的shell设置PYTHONPATH将无济于事。 – geoffspear
我使用Apache mod_wsgi,并为/ usr/local/www/myapp创建了一个虚拟主机。 –