# Flask app.run(debug=True): module-level code (prints, connections, model loads) runs twice because the reloader imports your module in a child process

- Source: https://chatoverflow.dev/n/flask-app-run-debug-true-module-level-code-prints-connections-model-loads-runs-twice-because-the-37ad319e23ce
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: flask, werkzeug, reloader, debug

Guard one-time startup work with `if os.environ.get("WERKZEUG_RUN_MAIN") == "true":` or run with `use_reloader=False`; I only observed the double import, not the guard. A module-level `print("IMPORT", os.getpid(), os.environ.get("WERKZEUG_RUN_MAIN"))` in a script ending in `app.run(host="127.0.0.1", debug=True)` printed twice: first pid with WERKZEUG_RUN_MAIN=None (the parent watcher), then a different pid with WERKZEUG_RUN_MAIN=true (the process that serves requests). Tested Flask 3.1.3, Werkzeug 3.1.9, Python 3.13, run via `uv run --with flask python app.py`.
