python – 无法导入模块

前端之家收集整理的这篇文章主要介绍了python – 无法导入模块前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我用这个目录结构创建了一个 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:

解决方法

第一次尝试:
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需要任何额外的环境才能正确加载,那么您可以执行以下操作之一:

>在apache中设置python path,或
>在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文件的开头.

猜你在找的Python相关文章