83

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)?

bkinsey808
  • 2,669
  • 3
  • 16
  • 20
  • Have you tried any of the solutions google offered? There's a lot so I don't know what you may or may not have found yet – Sterling Archer Mar 19 '18 at 02:35
  • well, i saw a program called "watchdog" but it looks like a programmatic library rather than a command line program like "nodemon". I'm just looking for something that works more or less out of the box like nodemon. – bkinsey808 Mar 19 '18 at 02:38
  • 1
    This question may be related: https://stackoverflow.com/questions/3274334/how-can-i-watch-a-file-for-modification-change – jrd1 Mar 19 '18 at 02:41
  • There are programs that will save the script upon running, which is pretty much the same thing. – 101 Mar 19 '18 at 02:46
  • it looks like pynotify might be what i need. but i'm having trouble installing it as a comman rather than a library – bkinsey808 Mar 19 '18 at 02:55
  • $python3 -m pynotify hello.py /usr/bin/python3: No module named pynotify.__main__; 'pynotify' is a package and cannot be directly executed – bkinsey808 Mar 19 '18 at 02:56
  • so the question is, how can i execute pynotify like a command, or is it only a library? – bkinsey808 Mar 19 '18 at 02:58
  • Got inspired to write a Docker image which integrates unittest, nose and nodemon (thanks, bkinsey808). Needs a little modification to suit your specific need. Find it [here](https://github.com/poppash/pynosemon). – poppash Apr 27 '19 at 13:57

6 Answers6

143

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`
bkinsey808
  • 2,669
  • 3
  • 16
  • 20
  • I am trying to use PyInquirer, when using `python` it works, but using `nodemon` gives me `assert self.stdin.isatty()` AssertionError. Using `nodemon` 2.0.3 – Mehdi Saffar May 11 '20 at 14:15
  • 11
    with `npx`'s ability to run node executables and wanting to clear the screen on each run, I've been using `npx nodemon --exec "clear;python3" hello.py`, which I discovered from https://github.com/remy/nodemon/issues/1318. Figured others might appreciate this approach as well. – Ehtesh Choudhury May 13 '20 at 06:13
  • @kd12345, yes, see answers – bkinsey808 Mar 25 '21 at 16:17
  • 1
    @bkinsey808 but how do i install it with pip? – kd12345 Mar 28 '21 at 13:28
  • @kd12345 nodemon is installed with nodejs. Install nodejs first, then `npm i -g nodemon` – bkinsey808 Mar 29 '21 at 16:39
  • @bkinsey808 i already have nodemon installed, should i just use it in django the same way i use it in node? – kd12345 Mar 30 '21 at 04:22
  • @kd12345 i think so. I'd have to see what you are doing to know better. – bkinsey808 Mar 30 '21 at 05:51
88

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.

Josh Dando
  • 1,647
  • 14
  • 9
  • 1
    I see. That must in the case where you use python3 in the commandline instead of just the python command. Good to know! – bkinsey808 Jan 23 '19 at 19:36
  • This does sometime cause issues where it fails to kill the subprocess and spawns a new one. Any suggestions? – pcnate Jul 01 '20 at 19:09
  • 1
    in windows you should use python instead of python3 -> in my case -> "nodemon --exec python server.py" – Shamshirsaz.Navid Jan 20 '21 at 06:51
  • this is what I ended up using but I wonder how to pass arguments like you would with `npm run python-script -- --verbose`. I want the verbose argument to get passed to python. Using `nodemon -e py -I --exec \"python server.py\"` – pcnate Mar 09 '21 at 20:09
25

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.

Jon Portella
  • 1,068
  • 9
  • 8
  • 3
    Perfect even for GUI applications `watchmedo auto-restart -p "*.py" -R python3 -- application.py`! Use `watchmedo auto-restart -h` to view the help. – Bergi Feb 09 '20 at 19:42
  • 1
    Seems to not work on Windows 10. `module 'os' has no attribute 'setsid'` – pcnate Jul 01 '20 at 19:10
7

I just use npx nodemon pythonfile.py and it works.Make sure you are using nodemon v2.0.x or above

prometheus
  • 96
  • 1
  • 6
2

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.

https://github.com/remy/nodemon#running-non-node-scripts

Jon Portella
  • 1,068
  • 9
  • 8
1

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 :

https://pypi.org/project/py-mon/

Devendra Singh
  • 140
  • 2
  • 6