3 ms·
Because of the nature of Python, using importlib.reload() is going to lead to terrible bugs at some point. Use importlib.reload() only in the shell, for conveni
by BiteCode_dev 6y ago
Because of the nature of Python, using importlib.reload() is going to lead to terrible bugs at some point. Use importlib.reload() only in the shell, for convenience.
If you want autoreload for dev (e.g: like django dev server), better restart the entire process.
There is a 7 years old project that can help you with that called watchdog:
https://pythonhosted.org/watchdog/
It's a library that uses the fastest way available (e.g: inotify,kqueue...) to react to file changes (you can filter by types such as create, delete, write, etc) and triggers whatever code you want, passing you the metadata.
If you just want something very simple, you don't have to code, you can install watchdog with its optional watchmedo dependancy:
pip install watchdog[watchmedo]
It will give you the watchmedo command, that lets you do something like this:
watchmedo shell-command --patterns="*.py" --recursive \
--command='python your_script_to_rerun.py' .
- stock_toaster 6y agoentr[1] is another good option. [1]: http://eradman.com/entrproject/ http://eradman.com/entrproject/
- BiteCode_dev 6y agoYes but unfortunatly, you have to compile it for many systems yourself. And if you want to use it from Python (not from the command line), you need binding. On the other hand, it's probably more performant than watchdog if you have a lot of files and need to fallback on manual watching. Plus, if you are not a Python person, you may not want to use pip.
- X-Istence 6y agoIf you are working on code that runs a web server or some other service that is generally a long running application, take a look at hupper: https://docs.pylonsproject.org/projects/hupper/en/latest/ https://docs.pylonsproject.org/projects/hupper/en/latest/ It is built specifically to restart things like servers as it was implemented for the Pylons Project (Pyramid web framework).
- BiteCode_dev 6y agoDoes hupper can restart on changes to non python files ?
- X-Istence 6y agoThere is an API to pass it non-python files (so that alongside your Python app it can watch your configuration files for instance/HTML files). However it does not work for just watching a directory + running a command, it is specifically for watching Python code.