Hot module reload for python
Project description
Python Hot Module Reload
Automatic reload your project when files are modified.
No need to modify your source code. Works at any environment.
Supported Syntax:
- ✅
import X
- ✅
from X import Y
Supported Types:
- ✅
Module
- ✅
Function
- ✅
Class
Installation
pip install python-hmr
Quick Start
Import your dev package as usual.
import my_pkg
Add 2 lines to automatically reload your source code.
import my_pkg
import hmr
my_pkg = hmr.reload(my_pkg)
Now you are ready to go!
Usage Manual
Module/Submodule reload
import my_pkg.sub_module as sub
import hmr
sub = hmr.reload(sub)
Function/Class reload
No difference to reloading module
from my_pkg import func, Class
import hmr
func = hmr.reload(func)
Class = hmr.reload(Class)
If your have multiple class instance, they will all be updated.
Both a
and b
will be updated.
a = Class()
b = Class()
@Decorated Function reload
Use functools.wraps to preserve signature of your function, or the function information will be replaced by the decorator itself.
State handling
If your application is not stateless, it's suggested that you
group all your state variable into the same .py
file like state.py
and exclude that from being reloaded.
Make sure you know what you are doing. This could lead to unexpected behavior and unreproducible bugs.
import my_pkg
import hmr
my_pkg = hmr.reload(my_pkg, excluded=["my_pkg.state"])
The my_pkg/state.py
will not be reloaded, the state will persist.
The same apply when reloading a function or class.
from my_pkg import func
import hmr
func = hmr.reload(func, excluded=["my_pkg.state"])
Acknowledgement
Inspired from the following package.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for python_hmr-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a35df72246d49ad24930049b26234711d9695f305f9716df88a8ecfdc32bed95 |
|
MD5 | fd26b4a0e9149d3e25918c9ca12f926d |
|
BLAKE2b-256 | 51b144e38e478283beeeaf8a07672b0c5d55c55599be3ec50c0aefbc17bcc90a |