Skip to content

app.run(debug=True),会导致flask_sqlalchemy错误! #5656

Answered by davidism
gitDebianD asked this question in Q&A
Discussion options

You must be logged in to vote

You're asking why code is running twice when using the debugger. Running the debugger runs the reloader. The reloader reloads at the very start to set things up. Your code executes, then is reloaded and executes again. You need to guard it with the following, similar to the if __name__ == "__main__" pattern.

from werkzeug.serving import is_running_from_reloader

if not is_running_from_reloader():
    # setup code that should only run once

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by davidism

This comment has been hidden.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #5655 on December 12, 2024 15:52.