Flask: Flask does not reload modified code when running under WSL

Created on 26 Jun 2020  路  5Comments  路  Source: pallets/flask

I am developing a Flask app on a Linux machine, which does correctly reload itself when I modify a Python source code file (when running in debug mode). Sometimes though I need to develop on a Windows machine, using WSL, but the very same Flask app sources. That basically works fine, however, under WSL the auto-reload feature does not seem to work any more. I can change the Python sources, but nothing happens - I need to manually shutdown and restart the Flask server.

Is this a known bug under WSL? Do I need to start the Flask application in a certain way?
As reference, here is how I start the application under VS Code:

"configurations": [
    {
        "name": "Run Server",
        "type": "python",
        "request": "launch",
        "module": "flask",
        "env": {
            "FLASK_APP": "run.py",
            "FLASK_ENV": "development",
        },
        "args": [
            "run"
        ],
    }
]

... which starts the the Flask appliacation under WSL with these output:

 * Serving Flask app "run.py" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with inotify reloader
 * Debugger is active!
 * Debugger PIN: 182-804-918

That output looks fine to me, but still no auto-reload.
Any ideas?

Environment

  • Python version: 3.8.2 64-bit (from Ubuntu on WSL)
  • Flask version: 1.1.2
  • Werkzeug version: 1.0.1

Most helpful comment

It's probably due to inotify not working well under the WSL. Try uninstalling the watchdog package, as that will force Flask to use the default "stat" reloader, which should behave better.

That solved my issue, after uninstalling watchdog auto-reloading worked now, thanks!

All 5 comments

It's probably due to inotify not working well under the WSL. Try uninstalling the watchdog package, as that will force Flask to use the default "stat" reloader, which should behave better.

You can also use --reloader stat for the same effect ;)

You can also use --reloader stat for the same effect ;)

How do I start Flask with this option (in VS Code)?
I tried this:

mfb@Sector2:/data/Python/MLserver$ FLASK_APP="run.py" FLASK_ENV="development" flask run --reloader stat
Usage: flask run [OPTIONS]
Try "flask run --help" for help.

Error: no such option: --reloader

nevermind, i somehow thought this option existed but apparently there's only --reload/--no-reload...

It's probably due to inotify not working well under the WSL. Try uninstalling the watchdog package, as that will force Flask to use the default "stat" reloader, which should behave better.

That solved my issue, after uninstalling watchdog auto-reloading worked now, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chuanconggao picture chuanconggao  路  4Comments

lnielsen picture lnielsen  路  3Comments

xliiv picture xliiv  路  3Comments

stillesjo picture stillesjo  路  4Comments

maangulo12 picture maangulo12  路  4Comments