Skip to main content

reloader.py

A simple Python hot-reloading tool that automatically reloads modules and scripts when they change, inspired by jurigged and built on watchdog.

Features

Core Features

  • Hot Reloading: Automatically reloads Python code when files change
  • State Preservation: Patches existing objects in-place, preserving application state
  • Module Support: Works with scripts, modules, and packages
  • Selective Reloading: Only reloads changed code chunks (functions, classes, statements)
  • Smart Detection: Uses AST parsing to identify and reload individual code components

Execution Modes

  • Script Mode: Run and reload Python scripts
  • Module Mode: Execute modules with -m flag
  • Function Mode: Run specific functions from modules
  • Code Mode: Execute code snippets with -c flag
  • Interactive Mode: Drop into interactive console after execution with -i
  • Loop Mode: Continuously re-execute code on changes with -l
  • Daemon Mode: Run reloader in background thread

Advanced Features

  • Watch Additional Paths: Monitor extra files/directories with -w
  • Clear Screen: Clear terminal before each reload with -C
  • Debouncing: Configurable delay to batch rapid file changes
  • Interrupt Handling: Gracefully interrupt long-running code during reload
  • Path Filtering: Exclude system paths from reloading
  • Import System Integration: Hooks into Python's import system via MetaPathFinder
  • Concurrent Monitoring: Efficiently monitors multiple files using watchdog

Code Analysis & Patching

  • AST-Based Parsing: Parses code into reloadable chunks
  • Function Patching: Updates __code__, __doc__, __annotations__, __defaults__
  • Class Patching: Preserves class instances while updating methods
  • Execution Tracking: Maintains counters to determine what needs reloading
  • Syntax Error Handling: Gracefully handles syntax errors without crashing

Installation

pip install reloader.py

Usage

Basic Usage

# Run a script with auto-reload
python -m reloader script.py

# Run with loop mode (continuously re-executes)
python -m reloader -l script.py

# Run a module
python -m reloader -m module_name

# Run a specific function
python -m reloader -m module_name:function_name

# Execute code snippet
python -m reloader -c "print('Hello, World!')"

# Interactive mode after execution
python -m reloader -i script.py

Advanced Usage

# Watch additional directories
python -m reloader -l -w /path/to/watch script.py

# Clear screen before each reload
python -m reloader -l -C script.py

# Custom debounce interval (seconds)
python -m reloader -d 0.5 script.py

# Run with arguments
python -m reloader script.py arg1 arg2

# Combine multiple options
python -m reloader -l -C -w ./config -w ./templates app.py

Command Line Options

Option Long Form Description
-i --interactive Enter interactive mode after script execution
-c --code Execute code string instead of script
-m --module Run module (optionally with :function)
-l --loop Enable loop mode for continuous execution
-w --watch Watch additional paths (requires --loop)
-C --clear Clear screen before each reload
-d --debounce Set debounce interval in seconds (default: 0.1)
-P --no-cwd-python-path Don't add current directory to Python path
--disallowed-path-types Exclude path types from reloading
--allowed-path-types Include specific path types for reloading
--version Show version information

How It Works

  1. File Monitoring: Uses watchdog to monitor file system events
  2. Code Parsing: Parses Python files into AST to identify code chunks
  3. Change Detection: Compares old and new code to find modifications
  4. Hot Patching: Updates existing objects without replacing them
  5. Execution: Re-executes changed code while preserving state

Architecture

Core Components

  • Watcher: Monitors file system changes using watchdog
  • CodeModule: Represents a Python module that can be reloaded
  • CodeChunk: Individual piece of code (function, class, or statement)
  • ModulePatcher: Handles the complex logic of patching existing objects
  • Reloader: Reload engine that applies file changes to watched modules

Reloader Modes

  • LoopReloader: Re-executes the target after each change (interrupting long-running scripts)
  • DaemonReloader: Runs the target once in the foreground while reloading in a daemon thread

Execution Targets

  • ScriptTarget: Runs a script file or module as __main__
  • FuncTarget: Runs a specific function from a module
  • CodeTarget: Evaluates a compiled code snippet (-c)
  • ConsoleTarget: Runs an interactive console (-i)

Limitations

  • Cannot reload certain built-in types (e.g., MemberDescriptor)
  • Changes to function signatures may require restart
  • Some module-level state may not be preserved perfectly
  • Threading and async code may have edge cases

Development

Built with:

  • Python 3.12+
  • watchdog for file monitoring
  • AST for code analysis
  • Threading for concurrent operations

License

MIT License

Credits

Inspired by jurigged and built on watchdog.

Author: EcmaXp

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reloader_py-0.19.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reloader_py-0.19.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file reloader_py-0.19.0.tar.gz.

File metadata

  • Download URL: reloader_py-0.19.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for reloader_py-0.19.0.tar.gz
Algorithm Hash digest
SHA256 a718a536109bf80f82eed2deb15abd57119c916dada755ff06f23b3fef426e87
MD5 636b54b6bfe6c0aaa75328b9e01bec0e
BLAKE2b-256 dc4cf658ded3cf78ab77a1570ad47897e00bf8fdf45a9a59a12dfbdd79d4ae5f

See more details on using hashes here.

Provenance

The following attestation bundles were made for reloader_py-0.19.0.tar.gz:

Publisher: python-publish.yml on EcmaXp/reloader.py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file reloader_py-0.19.0-py3-none-any.whl.

File metadata

  • Download URL: reloader_py-0.19.0-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for reloader_py-0.19.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a44b67039697351399197787ca1994feff6ffdf91f091fcbb28a81d87289b70e
MD5 341dcfa383194398863fa3774a3fdced
BLAKE2b-256 dcb0b996a484021b05ed1891c439a149b51a67e7e33252f088ea1ee4f06b5a2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for reloader_py-0.19.0-py3-none-any.whl:

Publisher: python-publish.yml on EcmaXp/reloader.py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.19.0 This release

2 files

0.18.0

2 files

0.17.1

2 files

0.17.0

2 files

0.16.3

2 files

0.16.2

2 files

0.16.1

2 files

0.16.0

2 files

0.15.1

2 files

0.15.0

2 files

0.14.3

2 files

0.14.2

2 files

0.14.1

2 files

0.14.0

2 files

0.13.0

2 files

0.12.3

2 files

0.12.2

2 files

0.12.1

2 files

0.12.0

2 files

0.11.1

2 files

0.11.0

2 files

0.10.4

2 files

0.10.3

2 files

0.10.2

2 files

0.10.1

2 files

0.10.0

2 files

0.9.1

2 files

0.9.0

2 files

0.8.13

2 files

0.8.12

2 files

0.8.11

2 files

0.8.10

2 files

0.8.9

2 files

0.8.8

2 files

0.8.7

2 files

0.8.6

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page