2

I'm using flask app factory pattern like and have this helloworld.py file

from flask import Flask

app = Flask(__name__)


@app.route('/')
def index():
    return 'This is the home page'


if __name__=="__name__":
    app.run(debug=True)

Then I run the app in Terminal :

python helloworld.py

(venv) C:\Users\Jayalakshmi.S1\myproject>python helloworld.py

(venv) C:\Users\Jayalakshmi.S1\myproject>

But when I go to http://localhost:5000 it doesn't work. It says:

Can’t reach this page

Make sure the web address http://127.0.0.1:5000 is correct

What could be wrong?

2 Answers 2

4

The problem is that you wrote if __name__=="__name__": instead of if __name__=="__main__":.

Since that will never be true, your app.run never happens. That's why when you run the script, it just returns immediately, instead of printing out something like * Running on http://127.0.0.1:5000/ and then waiting.

You also almost always want to run Flask this way:

set FLASK_APP=helloworld.py
flask run

… instead of:

python helloworld.py
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you...Thank you so much
Shouldn't this be export instead of set?!
@Cleb On Windows?
@Cleb Well, that's actually bash, not Unix… but close enough; most people on Unix systems (with a terminal) are either using bash, or know what they're doing (and anyone using bash on Windows knows what they're doing). But anyway, the OP is clearly on Windows, given the C:\Users\Jayalakshmi.S1\myproject>, so it shouldn't be export.
1

Your if condition is wrong. You should mention the main module which you're running...

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.