Skip to content

Commit 76fab50

Browse files
[Fix] Set Pythonpath for TestClusterExecutor (#907)
* [Fix] Set Pythonpath for TestClusterExecutor * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * external function * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add test for set_current_directory_in_environment --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 3ef0ba9 commit 76fab50

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

src/executorlib/standalone/command.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,15 @@ def generate_slurm_command(
162162
if slurm_cmd_args is not None and len(slurm_cmd_args) > 0:
163163
command_prepend_lst += slurm_cmd_args
164164
return command_prepend_lst
165+
166+
167+
def set_current_directory_in_environment():
168+
"""
169+
Add the current directory to the PYTHONPATH to be able to access local Python modules.
170+
"""
171+
environment = os.environ
172+
current_path = os.getcwd()
173+
if "PYTHONPATH" in environment and current_path not in environment["PYTHONPATH"]:
174+
environment["PYTHONPATH"] = os.getcwd() + ":" + environment["PYTHONPATH"]
175+
elif "PYTHONPATH" not in environment:
176+
environment["PYTHONPATH"] = os.getcwd()

src/executorlib/task_scheduler/file/spawner_pysqa.py

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

44
from pysqa import QueueAdapter
55

6+
from executorlib.standalone.command import set_current_directory_in_environment
67
from executorlib.standalone.hdf import dump, get_queue_id
78
from executorlib.standalone.inputcheck import check_file_exists
89
from executorlib.standalone.scheduler import pysqa_execute_command, terminate_with_pysqa
@@ -85,6 +86,7 @@ def execute_with_pysqa(
8586
os.path.dirname(os.path.abspath(cwd))
8687
)
8788
submit_kwargs.update(resource_dict)
89+
set_current_directory_in_environment()
8890
queue_id = qa.submit_job(**submit_kwargs)
8991
dump(file_name=file_name, data_dict={"queue_id": queue_id})
9092
return queue_id

src/executorlib/task_scheduler/file/spawner_subprocess.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
from typing import Optional
55

6+
from executorlib.standalone.command import set_current_directory_in_environment
67
from executorlib.standalone.hdf import dump
78
from executorlib.standalone.inputcheck import check_file_exists
89

@@ -53,11 +54,12 @@ def execute_in_subprocess(
5354
)
5455
if backend is not None:
5556
raise ValueError("backend parameter is not supported for subprocess spawner.")
56-
if resource_dict is None:
57-
resource_dict = {}
58-
cwd = resource_dict.get("cwd", cache_directory)
57+
cwd = _get_working_directory(
58+
cache_directory=cache_directory, resource_dict=resource_dict
59+
)
5960
if cwd is not None:
6061
os.makedirs(cwd, exist_ok=True)
62+
set_current_directory_in_environment()
6163
return subprocess.Popen(command, universal_newlines=True, cwd=cwd)
6264

6365

@@ -71,3 +73,14 @@ def terminate_subprocess(task):
7173
task.terminate()
7274
while task.poll() is None:
7375
time.sleep(0.1)
76+
77+
78+
def _get_working_directory(
79+
cache_directory: Optional[str] = None, resource_dict: Optional[dict] = None
80+
):
81+
if resource_dict is None:
82+
resource_dict = {}
83+
if "cwd" in resource_dict and resource_dict["cwd"] is not None:
84+
return resource_dict["cwd"]
85+
else:
86+
return cache_directory

tests/unit/standalone/test_command.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
import importlib.util
33
import sys
44
import unittest
5-
from executorlib.standalone.command import get_cache_execute_command, get_interactive_execute_command
6-
5+
from executorlib.standalone.command import (
6+
get_cache_execute_command,
7+
get_interactive_execute_command,
8+
set_current_directory_in_environment,
9+
)
710

811
skip_mpi4py_test = importlib.util.find_spec("mpi4py") is None
912

@@ -92,3 +95,18 @@ def test_get_cache_execute_command_parallel(self):
9295
get_cache_execute_command(cores=2, file_name=file_name, backend="flux", openmpi_oversubscribe=True)
9396
with self.assertRaises(ValueError):
9497
get_cache_execute_command(cores=2, file_name=file_name, backend="flux", exclusive=True)
98+
99+
def test_set_current_directory_in_environment(self):
100+
env = os.environ
101+
if "PYTHONPATH" in env:
102+
python_path = env["PYTHONPATH"]
103+
del env["PYTHONPATH"]
104+
else:
105+
python_path = None
106+
self.assertFalse("PYTHONPATH" in env)
107+
set_current_directory_in_environment()
108+
self.assertTrue("PYTHONPATH" in env)
109+
self.assertEqual(env["PYTHONPATH"], os.getcwd())
110+
env["PYTHONPATH"] = "/my/special/path"
111+
set_current_directory_in_environment()
112+
self.assertEqual(env["PYTHONPATH"], os.getcwd() + ":/my/special/path")

0 commit comments

Comments
 (0)