IIS 上带有 wfastcgi virtualenv 的 Flask 应用程序 - 找不到包

Flask App on IIS with wfastcgi virtualenv - Package not found

我已经为 运行 烧瓶服务器设置了一个 virtualenv。我已按照步骤设置 wfastcgi 以与 iis 一起工作。它似乎可以正常工作,因为我可以访问 python 应用程序,但会打印 python 错误堆栈。

读取 WSGI 处理程序时出错:

Traceback (most recent call last):
File "c:\python27\lib\site-packages\wfastcgi.py", line 793, in main
env, handler = read_wsgi_handler(response.physical_path)
File "c:\python27\lib\site-packages\wfastcgi.py", line 635, in read_wsgi_handler
handler = get_wsgi_handler(os.getenv("WSGI_HANDLER"))
File "c:\python27\lib\site-packages\wfastcgi.py", line 618, in get_wsgi_handler
raise ValueError('"%s" could not be imported%s' % (handler_name, last_tb))
ValueError: "flask_app.application()" could not be imported: Traceback (most recent call last):
File "c:\python27\lib\site-packages\wfastcgi.py", line 602, in get_wsgi_handler
handler = __import__(module_name, fromlist=[name_list[0][0]])
File ".\flask_app.py", line 7, in <module>
from app import app as application
File ".\app\__init__.py", line 67, in <module>
from flask_restless import APIManager
ImportError: No module named flask_restless

我认为这与我的安装方式有关flask_restless。我使用

安装它
pip install -e git://github.com/jfinkels/flask-restless.git#egg=flask_restless

似乎没有将 flask_restless 添加到 Lib/site-packages 目录。但是当我手动 运行 应用程序时,这不是问题。

这是我的 web.config:

<configuration>
  <system.webServer>
    <handlers>
      <add name="Python FastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\Python27\python.exe|C:\Python27\Lib\site-packages\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" />
    </handlers>
  </system.webServer>

  <appSettings>
    <!-- Required settings -->
    <add key="WSGI_HANDLER" value="flask_app.application()" />
    <add key="PYTHONPATH" value="C:\www\flask_app\virtualenv\src;C:\www\flask_app\virtualenv\Lib\site-packages;C:\www\flask_app\app" />

    <!-- Optional settings -->
    <add key="WSGI_LOG" value="C:\inetpub\logs\wsgi.log" />
    <add key="WSGI_RESTART_FILE_REGEX" value=".*((\.py)|(\.config))$" />
  </appSettings>
   </configuration>

而我的flask_app.py如下:

this_file = 'C:/www/flask_app/virtualenv/Scripts/activate_this.py'
with open(this_file) as f:
    code = compile(f.read(), this_file, 'exec')
    exec(code)


from app import app as application

我不知道这是否正确,但它确实对我有用。除了 flask-restless 之外,还有一些 wfastcgi 无法找到的不同包。所以为了解决这个问题,我最终激活了虚拟环境并打印出我的 sys.path 变量。然后,我将所有路径复制到我的 web.config 文件中的 PYTHONPATH 键中。

所以我的 web.config 现在看起来像这样:

<configuration>
<system.webServer>
<handlers>
  <add name="Python FastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\Python27\python.exe|C:\Python27\Lib\site-packages\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" />
</handlers>
</system.webServer>

<appSettings>
<!-- Required settings -->
<add key="WSGI_HANDLER" value="flask_app.application" />
<add key="PYTHONPATH" value="C:/www/Lib/site-packages;c:/python27/lib/site-packages/fabric-1.10.2-py2.7.egg;c:/python27/lib/site-packages/paramiko-1.16.0-py2.7.egg;c:/python27/lib/site-packages/ecdsa-0.13-py2.7.egg;c:/python27/lib/site-packages/pycrypto-2.6.1-py2.7-win32.egg;c:/python27/lib/site-packages/psycopg2-2.5.2-py2.7.egg;C:/Windows/system32/python27.zip;c:/python27/DLLs;c:/python27/lib;c:/python27/lib/plat-win;c:/python27/lib/lib-tk;c:/python27;c:/python27/lib/site-packages;C:/www/flask_app/virtualenv/src;C:/www/flask_app/virtualenv/Lib/site-packages;C:/www/flask_app/app;C:\www\flask_app\virtualenv\Lib\site-packages\Flask_Restless-1.0.0b1-py2.7.egg" />

<!-- Optional settings -->
<add key="WSGI_LOG" value="C:/www/flask_app/flask.log" />
<add key="WSGI_RESTART_FILE_REGEX" value=".*((\.py)|(\.config))$" />
</appSettings>
</configuration>