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
-mflag - Function Mode: Run specific functions from modules
- Code Mode: Execute code snippets with
-cflag - 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
- File Monitoring: Uses watchdog to monitor file system events
- Code Parsing: Parses Python files into AST to identify code chunks
- Change Detection: Compares old and new code to find modifications
- Hot Patching: Updates existing objects without replacing them
- 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 moduleCodeTarget: 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a718a536109bf80f82eed2deb15abd57119c916dada755ff06f23b3fef426e87
|
|
| MD5 |
636b54b6bfe6c0aaa75328b9e01bec0e
|
|
| BLAKE2b-256 |
dc4cf658ded3cf78ab77a1570ad47897e00bf8fdf45a9a59a12dfbdd79d4ae5f
|
Provenance
The following attestation bundles were made for reloader_py-0.19.0.tar.gz:
Publisher:
python-publish.yml on EcmaXp/reloader.py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
reloader_py-0.19.0.tar.gz -
Subject digest:
a718a536109bf80f82eed2deb15abd57119c916dada755ff06f23b3fef426e87 - Sigstore transparency entry: 2226635964
- Sigstore integration time:
-
Permalink:
EcmaXp/reloader.py@f61e6345536fd6a14d1b230b42a52076fe6c2d49 -
Branch / Tag:
refs/tags/0.19.0 - Owner: https://github.com/EcmaXp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@f61e6345536fd6a14d1b230b42a52076fe6c2d49 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a44b67039697351399197787ca1994feff6ffdf91f091fcbb28a81d87289b70e
|
|
| MD5 |
341dcfa383194398863fa3774a3fdced
|
|
| BLAKE2b-256 |
dcb0b996a484021b05ed1891c439a149b51a67e7e33252f088ea1ee4f06b5a2e
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
reloader_py-0.19.0-py3-none-any.whl -
Subject digest:
a44b67039697351399197787ca1994feff6ffdf91f091fcbb28a81d87289b70e - Sigstore transparency entry: 2226636393
- Sigstore integration time:
-
Permalink:
EcmaXp/reloader.py@f61e6345536fd6a14d1b230b42a52076fe6c2d49 -
Branch / Tag:
refs/tags/0.19.0 - Owner: https://github.com/EcmaXp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@f61e6345536fd6a14d1b230b42a52076fe6c2d49 -
Trigger Event:
release
-
Statement type: