简体   繁体   English

如何逐步运行Flask应用进行调试?

[英]How can I run my Flask app step by step to debug it?

I want to run IPython interactively while running my Flask app. 我想在运行Flask应用时以交互方式运行IPython。 I tried ipython -i app.py but I don't get a prompt until after the app exits. 我尝试了ipython -i app.py但直到应用程序退出后才收到提示。 I want to do this so I can debug each step of the program. 我想这样做,以便可以调试程序的每个步骤。 How can I run my app and be able to examine it? 如何运行我的应用程序并能够对其进行检查?

The -i flag runs the given program and then puts you in an interactive session after it has run . -i标志运行给定程序,然后在运行后进入交互式会话。 There is no way to debug the program using this flag. 无法使用此标志调试程序。

Instead, you want to use a debugger. 相反,您想使用调试器。 python -m pdb app.py will start pdb , a console debugger. python -m pdb app.py将启动控制台调试器pdb There are other debuggers available, such as the graphical one built into IDEs such as PyCharm and PyDev, or more advanced console ones such as pudb . 还有其他可用的调试器,例如内置在IDE中的图形化调试器(例如PyCharm和PyDev),或者更高级的控制台调试器(例如pudb)

Implemented with thread thread实现

from flask import Flask                                                         
import thread
data = 'foo'
app = Flask(__name__)
@app.route("/")
def main():
    return data
def flaskThread():
    app.run()
if __name__ == "__main__":
    thread.start_new_thread(flaskThread,())

And open the IPython Command prompt and type the command run -i filename.py 并打开IPython Command提示符,然后键入命令run -i filename.py

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

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