如何查看python源代码文件并在保存时重新启动? [英] How do i watch python source code files and restart when i save?
问题描述
当我保存一个python源代码文件时,我想重新运行脚本.有没有这样的命令(有点像 node 的 nodemon)?
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)?
推荐答案
虽然在 python 生态系统中可能有办法做到这一点,例如 watchdog/watchmedo ( https://github.com/gorakhargosh/watchdog ),甚至可能是带有 inotifywait 的 linux 脚本选项( https://linux.die.net/man/1/inotifywait ),对我来说,到目前为止最简单的解决方案是……只使用 nodemon!我不知道的是,虽然 nodemon 的 github 标语是监视 node.js 应用程序中的任何更改并自动重新启动服务器 - 非常适合开发",但实际上 nodemon 是一个非常通用的工具,并且知道 .py 文件应该例如用python执行.这就是我认为魔法发生的地方:https://github.com/remy/nodemon/blob/c1211876113732cbff78eb1ae10483eaaf77e5cf/lib/config/defaults.js
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`
这篇关于如何查看python源代码文件并在保存时重新启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!