Hosting a Flask app on IIS 7.5 - python

I'm trying to get a Flask app hosted onto an IIS server and I'm stumbling at the last section. Here are the steps I've taken so far:
Installed Python 2.6.6 and Flask 0.10.1
Installed IIS 7.5
Downloaded PyISAPIe 1.1.0
Created a new web site in IIS
Turned on "Enable 32-Bit Applications" for my newly created Application Pool.
Created a Wildcard Script Map using the PyISAPIe .dll
Created the following file and named it 'test.py'
from Http.WSGI import RunWSGI
from Http import Env
from datetime import datetime
from flask import Flask
app = Flask(__name__)
app.debug = True
#app.route('/', defaults={'path': ''})
#app.route('/')
def catch_all(path):
s = "Path: %s\nTime: %s" % (path, datetime.now())
return s
def Request():
RunWSGI(app)
Used Chrome to go to localhost/test.py which returned:
The problem is this: my app only runs if you go to /test.py, but I'd like to run regardless of the URL. What do I need to do within IIS so that all requests, regardless of the URL, will use my flask app?
UPDATE: I've got it working, sort of. Here's what I did:
Replaced the Request function in Http.Isapi.py with the following:
.
app_root = "my path"
sys.path.append(app_root)
from test import Request
Used Chrome to go to localhost/test.py which returned:
I now have a new problem. When I go from my test app to the actual app I am greeted with the following traceback:
File "C:\Python26\Lib\site-packages\sqlalchemy\connectors\pyodbc.py", line 50, in dbapi
return __import__('pyodbc')
ImportError: DLL load failed: The specified module could not be found.
The issue isn't that I don't have pyodbc installed. My app works perfectly fine if it's booted up through the Flask.run() method.

Related

Flask fails with "Error: While importing 'X', an ImportError was raised", but does not display the error. How to find the source of the error?

When starting a Flask app with:
$ flask run
I received the error:
Error: While importing 'wsgi', an ImportError was raised.
Usage: flask [OPTIONS] COMMAND [ARGS]...`
...
However, there is no stack trace or other information provided. What is the best way to get the ImportError stack trace?
Import the Flask app at the Python interpreter prompt
To see the ImportError stack trace, open a Python interpreter prompt and import the module that loads the Flask app (usually app.py or wsgi.py). If applicable, be sure that your virtual environment is activated.
$ python
>>> from my_app_folder import app
Set the FLASK_APP environment variable
If you can import the Flask app module using the Python interpreter without error, try setting the FLASK_APP environment variable to point to the Flask app module.
$ FLASK_APP='my_app_folder/app' FLASK_ENV=development flask run
This error can be caused if Flask is unable to import any libraries(in my case it was Flask_restful)
This is the workaround I found to find the missing libraries:-
I found which library was missing by just running the Flask App file (wsgi.py) directly with python
python wsgi.py
which gave an Importerror listing the missing libraries
after finding the missing libraries, simply install libraries using pip, for me Flask_restful was missing so I installed Flask_restful
.
After installing missing libraries simply run the flask app using
flask run
The only thing that I would add to Christopher Peisert's answer is the option that gave me the error messages that I was searching for:
(venv) ~/example_flask_app/$ python
>>> import app
Or in my case
>>> import microblog

Can not import a module when run the flask server using the flask run command

I'm new to flask framework and I want to write a simple flask app that uses another python module (librosa package). I have successfully installed librosa in the same virtual environment that I have installed flask and I can easily import it in the python interpreter. Here is the python script.
# app.py
from flask import Flask
import librosa
app = Flask(__name__)
#app.route('/')
def hello():
return 'Hello'
if __name__ == '__main__':
app.run()
The problem is that when I want to run the flask app using the flask run command (after setting export FLASK_APP=app.py) I get the ModuleNotFoundError error as follows:
Error: While importing "app", an ImportError was raised:
Traceback (most recent call last):
File "/home/masoud/anaconda3/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/home/masoud/projects/my_projects/seperator_rewriten/app.py", line 2, in <module>
import librosa
ModuleNotFoundError: No module named 'librosa'
The strange thing is that there is no import error when I run the flask server using python app.py command.
if you are using an IDE such as pycharm, then you may need to install it from the terminal in the IDE itself, not cmd
This may be the solution to your problem:
python -m flask run
The reason is that flask run may use the python executable somewhere else, not the virtual environment you created for the project.
You can add these lines of code in your script:
import sys
import flask
print(sys.executable)
print(flask.__version__)
And then you can check the python executable path using python app.py and flask run.

cannot import name 'Flask'

Im using window 10, I did the command pip install flask but I kept getting a ImportError: cannot import name 'Flask'. When I worked with flask couple months back it was running fine. Came back to run my old programs today and i get this error? I was just trying to run a simple html website.
from flask import Flask, render_template
from flask import request
app = Flask(__name__)
app.static_folder = 'static'
#app.route('/')
def index():
return render_template('index.html')
if __name__=='__main__':
app.run(debug = True)
Also before this error I had No module named 'Flask' so I did(found this in other stackoverflow post):
1. virtualenv
2. pip install flask (getting output that requirements are already satisfied)
3. Then I just try to run my flask which is called i.py and I get cannot import name 'Flask'. Went through many solutions on here still no idea how to fix it.
Basically i had 2 versions of python 3.6 and 3.5.2 when I deleted 3.6 its working fine.

How to install the python library - google-api-python-client-gae?

I'm trying to code a program to check files in Google Cloud Storage using Python.
Now I'm read the google doc -> https://developers.google.com/api-client-library/python/start/installation.
At the last step - "App Engine", it asks user to do following:
->> cd myproject
->> unzip google-api-python-client-gae-1.1.zip
So what's the next steps? I'm still having problem to run the Google sample python code about GCS, which throw error:
Traceback (most recent call last):
File "gcs_test.py", line 22, in <module>
from google.appengine.ext import webapp
ImportError: No module named google.appengine.ext
I tried to add below code but still got same error:
from sys import path
path.append(r"C:\Py_dev\gcs_test\google-api-python-client-gae-1.2.zip")
path.append(r"C:\Py_dev\gcs_test\C:\Py_dev\gcs_test\apiclient")
Thanks for all kind help!
google.appengine is the library used by App Engine apps, not by the api client library, the library you are looking for is apiclient
If you wish to run a normal python script instead of an App Engine app, you should remove references to App Engine specific libraries.
If you are running an app engine app, make sure you are using dev_appserver.py or the actual app engine servers, not just running the script using python

Google App Engine Remote API does not work from local client

This is using the Python SDK version 1.8.0.
My remote API works fine using remote_api_shell.py, but doesn't work when trying to accessing from within a python script. I'm using the sample code from google:
from google.appengine.ext.remote_api import remote_api_stub
import getpass
def auth_func():
return (raw_input('Username:'), getpass.getpass('Password:'))
remote_api_stub.ConfigureRemoteApi(None, '/_ah/remote_api', auth_func,
'localhost:8080')
and I'm also importing the fix_sys_path() from dev_appserver.py to set my sys.path correctly for the google app engine SDK:
import dev_appserver
dev_appserver.fix_sys_path()
that adds, among other paths, the following line to my sys.path:
'/google_appengine_1.8.0/lib/fancy_urllib'
However, the following error is thrown when the above call to remote_api_stub.ConfigureRemoteApi() is called:
opener.add_handler(fancy_urllib.FancyProxyHandler())
AttributeError: 'module' object has no attribute 'FancyProxyHandler'
Wait 7 years, and it all seems to work fine.

Categories

Resources