【问题标题】:Import modules with mod_wsgi使用 mod_wsgi 导入模块
【发布时间】:2013-10-24 12:48:32
【问题描述】:

我正在关注Setting up mod_wsgi on WampServer 上的一个简单教程,在我尝试导入之前一切正常,我尝试搜索网络,但没有得到有效的解决方案,这是一个错误问题吗(也许不是)或者肯定是配置问题?

如何在 mod_wsgi 中导入模块?

设置: windows8 64 / wampserver x64 bit / mod_wsgi x64bit

目录:

  1. Wampserver:c:/wamp
  2. wsgi 应用程序:c:/wamp/www/wsgi
  3. Python27 : c:/python27

wsgi 应用程序的别名:{按照教程中通过 wamp 的创建别名选项创建}

WSGIScriptAlias /wsgi/ "c:/wamp/www/wsgi/wsgi.py"
<Directory "c:/wamp/www/wsgi/">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
        Order allow,deny
    Allow from all
</Directory>

wsgi.py

from paste import httpserver

try :
    p = sys.argv[1]
    command = "from %(project)s import *" % {"project": p}
    exec(command)
    httpserver.serve(app, host='127.0.0.1', port='8080')

except :
    print "Usage: python runner.py <main_package>"
    sys.exit(0)

apache 错误日志:

  • 目标 WSGI 脚本 'C:/wamp/www/wsgi/wsgi.py' 无法加载为 Python 模块。
  • 处理 WSGI 脚本“C:/wamp/www/wsgi/wsgi.py”时发生异常。
  • 从粘贴导入 httpserver ImportError: cannot import name http服务器

但是,如果我将上面的代码更改为:

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'
    response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]

它打印输出。

选项尝试了 wsgi.py:

import os, sys
sys.path.append("c:\\wamp\\www\\wsgi\\paste\\")
sys.path.append(os.path.dirname(__file__))
sys.path.append('c:/wamp/www/wsgi/')
sys.path.insert(0, 'c:/wamp/www/wsgi')
sys.path.insert(1, 'c:/wamp/www')
sys.path.insert(0, "c:/wamp/www/wsgi/wsgi.py")
sys.path.insert(0, "c:/wamp/www/wsgi/paste")

编辑:粘贴模块位于应用根目录中。

【问题讨论】:

  • 为什么不将 Paste 安装到 Python 安装中或使用 Python 虚拟环境。将第三方包合并到您的项目中通常不是一个好主意。
  • 嗨@GrahamDumpleton 是的,我同意你的观点,在项目文件夹中包含第 3 方不是一个好主意。我确实在 Python 的 site-pakages 目录中有粘贴,如果这就是你的意思,并且我确实将它包含在我的应用程序目录中,因为应用程序会引发导入错误;但这也无济于事,我不想使用 virtualenv;因为我正在学习检查如何在没有 virtualenv 的情况下使其工作。

标签: python python-2.7 apache2 mod-wsgi wampserver


【解决方案1】:

您没有提到您已将 Python 激活为 Apache 中的有效 CGI。

这有帮助吗 -> Configure Apache to use Python

【讨论】:

    【解决方案2】:

    尝试删除最后的斜线:

      WSGIScriptAlias /wsgi "c:/wamp/www/wsgi/wsgi.py"
    

    代替:

      WSGIScriptAlias /wsgi/ "c:/wamp/www/wsgi/wsgi.py"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多