How do i watch python source code files and restart when i save?
Question:
When I save a python source code file, I want to re-run the script. Is there a command that works like this (sort of like nodemon for node)?
Answers:
While there are probably ways to do this within the python ecosystem such as watchdog/watchmedo ( https://github.com/gorakhargosh/watchdog ), and maybe even linux scripting options with inotifywait ( https://linux.die.net/man/1/inotifywait ), for me, the easiest solution by far was… to just use nodemon! What I didn’t know is that although the github tagline of nodemon is “Monitor for any changes in your node.js application and automatically restart the server – perfect for development” actually nodemon is a delicously generic tool and knows that .py files should be executed with python for example. Here’s where I think the magic happens: https://github.com/remy/nodemon/blob/c1211876113732cbff78eb1ae10483eaaf77e5cf/lib/config/defaults.js
End result is that the command line below totally works. Yay!
$ nodemon hello.py
[nodemon] starting `python hello.py`
You can install nodemon to watch for file changes.
e.g.
npm i -g nodemon
Then to use:
nodemon --exec python3 hello.py
This is for when you use python3 in the command line. On windows you can also use ‘py’ instead.
The most similar way to nodemon I found is by using the watchdog package:
pip install watchdog
This comes with a utility called watchmedo:
watchmedo shell-command
--patterns="*.py"
--command='python "${watch_src_path}"'
.
Now just work on your .py
and it will be executed every time you save the file.
You actually can use nodemon with python, from their docs:
Running non-node scripts nodemon can also be used to execute and
monitor other programs. nodemon will read the file extension of the
script being run and monitor that extension instead of .js if there’s
no nodemon.json:
nodemon --exec "python -v" ./app.py
Now nodemon will run app.py with python in verbose mode (note that if
you’re not passing args to the exec program, you don’t need the
quotes), and look for new or modified files with the .py extension.
I just use npx nodemon pythonfile.py
and it works.Make sure you are using nodemon v2.0.x
or above
I have used py-mon to watch for file changes.
Installation
pip install py-mon
Execution
pymon filename.py
below is the link of package :
When I save a python source code file, I want to re-run the script. Is there a command that works like this (sort of like nodemon for node)?
While there are probably ways to do this within the python ecosystem such as watchdog/watchmedo ( https://github.com/gorakhargosh/watchdog ), and maybe even linux scripting options with inotifywait ( https://linux.die.net/man/1/inotifywait ), for me, the easiest solution by far was… to just use nodemon! What I didn’t know is that although the github tagline of nodemon is “Monitor for any changes in your node.js application and automatically restart the server – perfect for development” actually nodemon is a delicously generic tool and knows that .py files should be executed with python for example. Here’s where I think the magic happens: https://github.com/remy/nodemon/blob/c1211876113732cbff78eb1ae10483eaaf77e5cf/lib/config/defaults.js
End result is that the command line below totally works. Yay!
$ nodemon hello.py
[nodemon] starting `python hello.py`
You can install nodemon to watch for file changes.
e.g.
npm i -g nodemon
Then to use:
nodemon --exec python3 hello.py
This is for when you use python3 in the command line. On windows you can also use ‘py’ instead.
The most similar way to nodemon I found is by using the watchdog package:
pip install watchdog
This comes with a utility called watchmedo:
watchmedo shell-command
--patterns="*.py"
--command='python "${watch_src_path}"'
.
Now just work on your .py
and it will be executed every time you save the file.
You actually can use nodemon with python, from their docs:
Running non-node scripts nodemon can also be used to execute and
monitor other programs. nodemon will read the file extension of the
script being run and monitor that extension instead of .js if there’s
no nodemon.json:
nodemon --exec "python -v" ./app.py
Now nodemon will run app.py with python in verbose mode (note that if
you’re not passing args to the exec program, you don’t need the
quotes), and look for new or modified files with the .py extension.
I just use npx nodemon pythonfile.py
and it works.Make sure you are using nodemon v2.0.x
or above
I have used py-mon to watch for file changes.
Installation
pip install py-mon
Execution
pymon filename.py
below is the link of package :