|
| 1 | +# Fix for ModuleNotFoundError: No module named 'julia_utils' |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +The application was crashing with the following error when using Monarch actors: |
| 6 | + |
| 7 | +``` |
| 8 | +ModuleNotFoundError: No module named 'julia_utils' |
| 9 | +``` |
| 10 | + |
| 11 | +This error occurred when remote Monarch actors tried to unpickle function references that were loaded from the `julia_utils` module. |
| 12 | + |
| 13 | +## Root Cause |
| 14 | + |
| 15 | +The issue happened because: |
| 16 | + |
| 17 | +1. The main process loads functions from `julia_utils` using `load_function_from_string()` |
| 18 | +2. These functions are passed as parameters to actor classes (`GenericDatasetActor`, `GenericRewardActor`) |
| 19 | +3. When actors are spawned as remote actors, the function objects are pickled and sent to remote processes |
| 20 | +4. During unpickling, Python needs to import the `julia_utils` module |
| 21 | +5. **The openenv directory wasn't in `sys.path` yet** because: |
| 22 | + - The unpickling happens during actor initialization (when deserializing constructor parameters) |
| 23 | + - The `setup()` endpoint runs AFTER actor initialization |
| 24 | + - Therefore, `sys.path` wasn't modified before unpickling occurred |
| 25 | + |
| 26 | +## Solution |
| 27 | + |
| 28 | +Added module-level code to `/home/kaiwu/work/kaiwu/forge/apps/openenv/main.py` that adds the openenv directory to `sys.path` BEFORE any actor definitions: |
| 29 | + |
| 30 | +```python |
| 31 | +# CRITICAL: Add openenv directory to sys.path at module level |
| 32 | +# This ensures that when remote actors unpickle function references (e.g., julia_utils functions), |
| 33 | +# the module can be imported successfully. This must happen BEFORE any actor definitions. |
| 34 | +_openenv_dir = Path(__file__).parent |
| 35 | +if str(_openenv_dir) not in sys.path: |
| 36 | + sys.path.insert(0, str(_openenv_dir)) |
| 37 | +``` |
| 38 | + |
| 39 | +This code runs when the module is first imported, ensuring that: |
| 40 | +- Remote actors that import `main.py` will have the openenv directory in their `sys.path` |
| 41 | +- Functions from `julia_utils` can be successfully unpickled in remote processes |
| 42 | +- The fix happens early enough to prevent the ModuleNotFoundError |
| 43 | + |
| 44 | +## Testing |
| 45 | + |
| 46 | +Created comprehensive tests to verify the fix: |
| 47 | + |
| 48 | +1. **test_module_import.py** - Tests basic import and pickling functionality |
| 49 | +2. **test_monarch_actor_simulation.py** - Simulates the exact Monarch actor scenario where a remote process receives pickled functions |
| 50 | + |
| 51 | +Both test suites pass successfully, confirming that: |
| 52 | +- `julia_utils` can be imported after importing `main.py` |
| 53 | +- Functions from `julia_utils` can be pickled and unpickled across process boundaries |
| 54 | +- Remote actors can successfully deserialize function references |
| 55 | + |
| 56 | +## Files Modified |
| 57 | + |
| 58 | +- `/home/kaiwu/work/kaiwu/forge/apps/openenv/main.py` - Added module-level sys.path setup |
| 59 | + |
| 60 | +## Files Added |
| 61 | + |
| 62 | +- `/home/kaiwu/work/kaiwu/forge/apps/openenv/test_module_import.py` - Basic import/pickle tests |
| 63 | +- `/home/kaiwu/work/kaiwu/forge/apps/openenv/test_monarch_actor_simulation.py` - Comprehensive simulation tests |
| 64 | + |
| 65 | +## Verification |
| 66 | + |
| 67 | +Run tests to verify the fix: |
| 68 | +```bash |
| 69 | +cd /home/kaiwu/work/kaiwu/forge/apps/openenv |
| 70 | +python test_module_import.py |
| 71 | +python test_monarch_actor_simulation.py |
| 72 | +``` |
| 73 | + |
| 74 | +Both should show "✓ All tests passed!" |
0 commit comments