简体   繁体   English

Python Flask app.run 不可访问,IP 未显示在终端中

[英]Python Flask app.run is not rechable, IP not showing in terminal

When I run my flask with pycharm python code run well but when it come to app.run() my terminal doesnot show any ip address it directly say unreachable code. When I run my flask with pycharm python code run well but when it come to app.run() my terminal doesnot show any ip address it directly say unreachable code. I uninstall my python and pycharm and reinstall them the problem is same.I try with visual studio code but the error is same what can i do?我卸载了我的 python 和 pycharm 并重新安装它们,问题是一样的。我尝试使用 Visual Studio 代码,但错误是一样的,我该怎么办? enter image description here在此处输入图像描述

Your code is close but wrong.您的代码很接近但错误。 The error is because of the indentation.错误是因为缩进。 You have added the debug=True to the route, it should be indented from if __name__ == '__main__' like this:您已将debug=True添加到路由中,它应该从if __name__ == '__main__'缩进,如下所示:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello, World!'

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

If you are curious about why you should bother with the addition of the if __name__ == '__main__': line then check out this great explanation.如果您对为什么要添加if __name__ == '__main__':行感到好奇,请查看这个很好的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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