Skip to content

Commit d7324d0

Browse files
committed
reload sys.path on job start #12
1 parent 32d7d4d commit d7324d0

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

submitit/core/submission.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import argparse
88
import os
9+
import sys
910
import time
1011
import traceback
1112
from pathlib import Path
@@ -49,6 +50,8 @@ def process_job(folder: Union[Path, str]) -> None:
4950
)
5051
try:
5152
delayed = utils.DelayedSubmission.load(paths.submitted_pickle)
53+
logger.info(f"Set sys.path to {delayed.sys_path} like in the scheduler runtime.")
54+
sys.path = list(delayed.sys_path)
5255
env = job_environment.JobEnvironment()
5356
env._handle_signals(paths, delayed)
5457
result = delayed.result()

submitit/core/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def __init__(self, function: Callable[..., Any], *args: Any, **kwargs: Any) -> N
125125
self._done = False
126126
self._timeout_min: int = 0
127127
self._timeout_countdown: int = 0 # controlled in submission and execution
128+
self.sys_path = tuple(sys.path)
128129

129130
def result(self) -> Any:
130131
if self._done:

submitit/test_pickle.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#
66

77
import pickle
8+
import subprocess
9+
import sys
10+
from pathlib import Path
811
from weakref import ref
912

1013
import pytest
@@ -78,3 +81,28 @@ def get_main() -> str:
7881
ex = LocalExecutor(tmp_path)
7982
j_main = ex.submit(get_main).result()
8083
assert main == j_main
84+
85+
86+
def test_submitit_respects_sys_path(tmp_path: Path):
87+
# https://github.com/facebookincubator/submitit/issues/12
88+
CUSTOM_CODE = f"""
89+
import submitit
90+
import sys
91+
from pathlib import Path
92+
93+
def dump_sys_path(file):
94+
Path(file).write_text("\\n".join(sys.path))
95+
96+
dump_sys_path("{tmp_path}/scheduler_path.txt")
97+
ex = submitit.LocalExecutor("{tmp_path}/log")
98+
job = ex.submit(dump_sys_path, "{tmp_path}/job_path.txt")
99+
job.wait()
100+
101+
"""
102+
scheduler_py = tmp_path / "scheduler.py"
103+
scheduler_py.write_text(CUSTOM_CODE)
104+
subprocess.check_call([sys.executable, scheduler_py])
105+
job_sys_path = (tmp_path / "job_path.txt").read_text()
106+
scheduler_sys_path = (tmp_path / "scheduler_path.txt").read_text()
107+
108+
assert job_sys_path == scheduler_sys_path

0 commit comments

Comments
 (0)