Skip to main content

Hot module reload for python

Project description

Python Hot Module Reload

python-hmr logo

Better debugging experience with HMR

Test status pypi license-mit Endpoint Badge

Automatic reload your project when files are modified.

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

reload

Supported Syntax:

  • import X
  • from X import Y

Supported Types:

  • Module
  • Function
  • Class

Installation

pip install python-hmr

Quick Start

[!CAUTION] From v0.3.0, there is only one API, hmr.reload.

Import your dev packages as usual. And add 2 lines for automatically reloading.

import dev

import hmr
dev = hmr.reload(dev)

If you have multiple modules to reload, you can do it like this.

from dev import run1, run2

import hmr
run1, run2 = hmr.reload(run1, run2)

Now you are ready to go! Try to modify the run1 or run2 and see the magic happen.

Detailed Usage

Function/Class instance

When you try to add HMR for a function or class, remember to pass the name of the function or class instance without parenthesis.

from dev import Runner

import hmr
Runner = hmr.reload(Runner)

a = Runner()
b = Runner()

[!IMPORTANT] Here, when both a and b will be updated after reloading. This may be helpful if you have an expansive state store within the class instance.

However, it's suggested to reinitialize the class instance after reloading.

@Decorated Function

Use functools.wraps to preserve signature of your function, or the function information will be replaced by the decorator itself.

import functools

def work(f):
    @functools.wraps(f)
    def args(*arg, **kwargs):
        return f(*arg, **kwargs)

    return args

Stateful application

If your application is stateful, you can exclude the state from being reloaded. For simplicity, you can 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 dev

import hmr
dev = hmr.reload(dev, exclude=["dev.state"])

In this way dev/state.py will not be reloaded, the state will persist.

This also apply when reloading a function or class.

from dev import run

import hmr
run = hmr.reload(run, exclude=["dev.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.3.0.tar.gz (831.3 kB view details)

Uploaded Source

Built Distribution

python_hmr-0.3.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file python_hmr-0.3.0.tar.gz.

File metadata

  • Download URL: python_hmr-0.3.0.tar.gz
  • Upload date:
  • Size: 831.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for python_hmr-0.3.0.tar.gz
Algorithm Hash digest
SHA256 5ac20308904d0aa4fe2ef9d48a2e90537b9140490861917e62e63fb76bdc1543
MD5 3e5b069710623b583f5b8940f5d44be7
BLAKE2b-256 4882772e678f85da8a9dd44b9999a40eba64888e47fec86eec2b198f37462730

See more details on using hashes here.

File details

Details for the file python_hmr-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: python_hmr-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for python_hmr-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e9c70f6890b6fa9ca03ec8cae08ef93017ce2b4ab908d7989ab1986a78677164
MD5 2a918d88de134c4b7b540686adb2f5e4
BLAKE2b-256 ed850378d09e062e3769761d8ca4880c2f98196385b9f1d4c255d71686690ab9

See more details on using hashes here.

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