Skip to content

Commit dbf89d8

Browse files
committed
reload sys.path on job start #12
1 parent f76c663 commit dbf89d8

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
@@ -5,6 +5,7 @@
55
#
66

77
import argparse
8+
import sys
89
import time
910
import traceback
1011
from pathlib import Path
@@ -42,6 +43,8 @@ def process_job(folder: Union[Path, str]) -> None:
4243
)
4344
try:
4445
delayed = utils.DelayedSubmission.load(paths.submitted_pickle)
46+
logger.info(f"Set sys.path to {delayed.sys_path} like in the scheduler runtime.")
47+
sys.path = delayed.sys_path
4548
env = job_environment.JobEnvironment()
4649
env._handle_signals(paths, delayed)
4750
result = delayed.result()

submitit/core/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def __init__(self, function: Callable[..., Any], *args: Any, **kwargs: Any) -> N
117117
self._result: Any = None
118118
self._done = False
119119
self.timeout_countdown: int = 0 # controlled in submission and execution
120+
self.sys_path = list(sys.path)
120121

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

0 commit comments

Comments
 (0)