pythonflaskwatchdog

Flask not auto-reloading when running under WSL2 and I do not have 'watchdog' installed


I am writing a Flask app, but it does not auto-refresh when I save my code changes, I have not watchdog installed, so this answer does not work for me! Somebody can help me?

By the way, my code is just a file main.py with the following code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "<h1>Hello Stack Overflow!</h1>"

if __name__ == "__main__":
    app.run(debug=True, port=8888)

My environment variables are:

export FLASK_APP=main.py
export FLASK_ENV=development
export FLASK_RUN_PORT=8888
export FLASK_DEBUG=1

Solution

  • I ended up changing my entire project (this one and all my workflow for all the following projects) to my WSL2 file system; is faster, I can work with Neovim and it is very good integrated with all Linux terminal based tools (and even GUI tools).

    This point means that, if you are working on Windows, and you are using Windows Subsystem for Linux, you should not work on paths like /mnt/c/Users/<user_name>/<any_path> (i.e. working within the traditional Windows file system) you should always stay on ~/<any_path> that is the same as /home/<user_name>/<any_path> this route being the WSL2 file system. So for example, the original project I was working on years ago when I asked this question, I first had every file on my Windows System in some path like /mnt/c/User/david/OneDrive/Documents/FlaskProjects/AmazingProject/ but, as this docs says, this was causing a bunch of errors and having a lot performance issues because, entering this path from WSL2 is entering a mounted drive, so the performance and Linux integrations is worst in all cases (it happened to me again trying to work with Docker). I learnt this the bad way struggling a lot, I do not want you suffer like me, and that is why my recommendation (and actually Microsoft recommendation) is to always use WSL2 file system if you are working with WSL2 or WSL in general, i.e. change your projects from /mnt/c/User/<user_name>/<some_windows_path> (Windows) to /home/<user_name>/<some_wsl_path> (Linux) and your developing experience would be better.