Skip to main content

Hot module reload for python

Project description

python-hmr logo

Python Hot Module Reload

Test status pypi license-mit

Automatic reload your project when files are modified.

No need to modify your source code. Works at any environment.

reload

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

python-hmr-0.2.0.tar.gz (516.2 kB view hashes)

Uploaded Source

Built Distribution

python_hmr-0.2.0-py3-none-any.whl (5.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page