简体   繁体   中英

How do I fix @app.route syntax error with Flask?

from flask import Flask

app = Flask(__name__)

@app.route('/')

def hello_world():
    return 'Hello from Flask!'

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

But Python is prompting the following error:

@app.route('/')
...
  File "<stdin>", line 2

    ^
SyntaxError: invalid syntax

I ran your code and it worked for me. There does not seem to be a clear syntax error.

Try saving your file again and clearing your terminal before running it.

Another alternative is running the following in your command prompt before running your code:

set FLASK_APP={name of your file}.py 

In the command prompt it should look something like this:

C:\path\to\app>set FLASK_APP=hello.py

Lastly, you can also try running it by writing the following in your command prompt or terminal instead of using the main() function you have now:

python -m flask run

More details and alternatives here: https://flask.palletsprojects.com/en/1.1.x/quickstart/

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM