我用这个目录结构创建了一个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 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:
答案 0 :(得分:8)
首先尝试:
python /usr/local/www/myapp/myapp.wsgi
是否正确加载?
如果是,那么您的应用可能需要一些环境(~/.bashrc
或类似)。尝试::
# to wipe-out extra env
env -i bash
# try again
python /usr/local/www/myapp/myapp.wsgi
验证你在shell中使用与apache WSGI使用的python相同的python。
如果您的myapp.wsgi
需要任何额外的环境才能正确加载,那么您可以执行以下操作之一:
要在WSGI代码中设置,这是示例代码。
import os, sys
EXTRA_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..'))
if EXTRA_DIR not in sys.path:
sys.path.append(EXTRA_DIR)
放入myapp.wsgi文件的开头。
答案 1 :(得分:1)
__init.__py
文件夹中有layout
,但它应为__init__.py
。这个时期错了。我不确定这是否是你帖子中的拼写错误,但如果这就是你的文件看起来会导致这个问题。
答案 2 :(得分:0)
modules目录还需要将__init__.py
文件定义为包。
答案 3 :(得分:0)
我有一个类似的问题,这解决了它:
chmod a+x myapp.wsgi
答案 4 :(得分:0)
根据http://webpy.org/install#apachemodwsgi
如果在apache error.log文件中出现“ImportError:No module named web”,您可以尝试在导入web之前在code.py中设置绝对路径:
import sys, os
abspath = os.path.dirname(__file__)
sys.path.append(abspath)
os.chdir(abspath)
import web
它非常适合我。