Flask auto reload: restart the dev server on code changes
Codemia Knowledge Hub ·
Run Flask in debug mode and the built-in reloader restarts the development server whenever you change a Python source file. Use flask run --debug, pass debug=True to app.run(), or set FLASK_DEBUG=1. The reloader comes from Werkzeug. Non-Python files may need extra configuration, and debug mode must never be enabled in production.
Option 1: app.run(debug=True)
debug=True enables both the auto-reloader and the interactive debugger. When you save a .py file, the server restarts within about a second.
Option 2: flask run --debug (recommended)
The flask run CLI is the recommended way to start Flask applications. --debug enables both auto-reload and the debugger. --reload enables only the reloader, without the debugger.
Option 3: environment variables in a .env file
A .env file keeps debug configuration out of your source code. Flask loads it automatically when python-dotenv is installed.
stat vs watchdog reloader
The default stat reloader polls files every second, which can be slow on large projects. The watchdog reloader uses OS file system events (inotify on Linux, FSEvents on macOS) for near-instant detection.
Watching extra files
By default, Flask watches only .py files. Jinja2 template changes show up without a restart because templates are re-rendered on each request. Configuration files and other non-Python files need extra_files.
Live reload inside Docker
The volume mount (- .:/app) maps your local source into the container, so the reloader inside the container sees your edits and restarts.
Turning the reloader off
Disable the reloader when the restart itself causes problems, for example dropped database connections, or when attaching an external debugger such as PyCharm or VS Code.
Reloader problems and how to fix them
- Debug mode in production:
debug=Trueexposes the Werkzeug interactive debugger, which allows arbitrary Python code execution from the browser. This is a critical security vulnerability. Use environment variables so debug mode is only active in development. - Changes to non-.py files are not detected: templates are re-rendered per request, but
.yaml,.jsonand other config files need--extra-filesorextra_files=. - Restart loop after a syntax error: if a syntax error occurs during import, the reloader may fail to restart cleanly. Fix the error and it recovers. If it does not, stop and restart the server manually.
- Port already in use after a crash: the port (5000) may stay bound. Run
lsof -i :5000(macOS/Linux) ornetstat -ano | findstr :5000(Windows) to find and kill the orphaned process. - Startup code runs twice: the Werkzeug reloader spawns a child process that runs your app again, so initialization such as database setup or starting a scheduler executes in both processes. Werkzeug provides
werkzeug.serving.is_running_from_reloader(), which tells you whether the current process is the reloader's subprocess. Flask'sbefore_first_requestwas deprecated in 2.2 and removed in 2.3, so run setup code when creating the application (for example in an app factory) instead.
Related reading
- Automatic creation date for Django model form objects
- Automatically create file 'requirements.txt
- Automatically document all modules recursively with Sphinx autodoc
- Autoreload of modules in IPython
- ''AutoTrackable'' object is not callable in Python
- Average values of dictionaries
- Avoid overflow with softplus function in python
- Avoid tensorflow print on standard error
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.