Watch Python Project for Changes

Productivity is vital for web developers because there are many distractions that can disrupt their workflow. To maintain focus and make the most of their time, it is important for web developers to minimize the number of switches between tasks as much as possible. Constant task switching can lead to reduced efficiency and productivity, as it takes time for the brain to refocus and adapt to new tasks. By minimizing task switching and focusing on one task at a time, web developers can increase their productivity and deliver high-quality work.

As things stand, web developers have to watch for changes in their code and manually refresh their browser or development server to see the changes.

Node.js community solved this issue through a package called nodemon years ago, but Python stills lacks some reliable package for this purpose.

In this article, I will show you how to watch Python files for changes and automatically reload your development server.

As it turns out, the best solutions is exactly what comes from Node.js world, nodemon.

Our first step is to install nodemon globally.

npm install -g nodemon

-g flag installs nodemon globally, so you can use it from anywhere.

I had used following command for years while developing Python projects, but there is a catch. It will only watch your main file which in my case it was always app.py.

nodemon --exec "python3 app.py"

Sometimes when you work on let’s say a flask project you change multiple file simultaneously and it is quite hard to restart server each time.

After a few hours of searching, I found out that you can customize the source and extensions when using nodemon

nodemon --ext html,py --exec "python3 app.py" --watch './**/*'