Monday, September 21, 2015

`Hello, world!` by Flask in Linux

Install pip if not installed:

sudo yum install python-pip

Install Flask if not installed:

sudo pip install Flask

Don't make your sample filename as `flask.py`.

If you do so, you will get the following error:

Traceback (most recent call last):
  File "flask.py", line 1, in <module>
    from flask import Flask
  File "/home/izeye/workspaces/izeye/python/flask.py", line 1, in <module>
    from flask import Flask
ImportError: cannot import name Flask

In `test_flask.py`:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
  return "Hello, world!"

if __name__ == "__main__":
  app.run()

You can run it as follows:

python test_flask.py

References:
http://flask.pocoo.org/
http://stackoverflow.com/questions/14792605/python-flask-import-error

No comments:

Post a Comment