roemhildtg roemhildtg - 1 month ago 53
Python Question

Flask App on IIS with wfastcgi virtualenv - Package not found

I've set up a virtualenv to run a flask server. I have followed the steps to set up wfastcgi to work with iis. It seems to be working, as I am able to access the python app, but a python error stack is printed.

Error occurred while reading WSGI handler:

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


I think this has to do with the way I installed flask_restless. I installed it using

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


which doesn't appear to add flask_restless to the Lib/site-packages directory. But its not an issue when I run the app manually.

Here's my 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>


And my flask_app.py is as follows:

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

Answer

I have no idea if this is correct, but it did work for me. There were a few different packages that couldn't be found by wfastcgi, besides flask-restless. So to solve this, I ended up activating the virtual env and printing out my sys.path variable. I then copied all of the paths into the PYTHONPATH key in my web.config file.

So my web.config now looks like this:

<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>