Skip to content

Commit 6232621

Browse files
authored
[core] Update sys.path to ensure that import lib works, thereby fixing test_job_isolation (#51765)
This PR fixes #51738. I found that `sys.path` is different when we run `test_job.py` with `pytest` and `bazel test //python/ray/tests:test_job` in CI runners (at least the one for Python 3.12). For the bazel command, `tmpdir/v1` is not in `sys.path`, so `import lib` fails. --------- Signed-off-by: kaihsun <[email protected]>
1 parent 8539a97 commit 6232621

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

Diff for: python/ray/tests/test_job.py

+25-15
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def test_job_isolation(ray_start_regular):
6060
# Make sure two jobs with same module name
6161
# don't interfere with each other
6262
# (https://github.com/ray-project/ray/issues/19358).
63+
6364
gcs_address = ray_start_regular.address_info["gcs_address"]
6465

6566
lib_template = """
@@ -74,27 +75,36 @@ def subtask():
7475
"""
7576
driver_template = """
7677
import ray
78+
import os
79+
import sys
80+
81+
# Add current directory to Python path so we can import lib.py
82+
current_dir = os.path.dirname(os.path.abspath(__file__))
83+
sys.path.insert(0, current_dir)
84+
7785
import lib
7886
7987
ray.init(address="{}")
8088
assert ray.get(lib.task.remote()) == {}
8189
"""
90+
91+
def setup_driver_files(base_path: str, version: int) -> str:
92+
version_path = os.path.join(base_path, f"v{version}")
93+
os.makedirs(version_path)
94+
95+
lib_path = os.path.join(version_path, "lib.py")
96+
driver_path = os.path.join(version_path, "driver.py")
97+
98+
with open(lib_path, "w") as f:
99+
f.write(lib_template.format(version))
100+
with open(driver_path, "w") as f:
101+
f.write(driver_template.format(gcs_address, version))
102+
103+
return driver_path
104+
82105
with tempfile.TemporaryDirectory() as tmpdir:
83-
os.makedirs(os.path.join(tmpdir, "v1"))
84-
v1_lib = os.path.join(tmpdir, "v1", "lib.py")
85-
v1_driver = os.path.join(tmpdir, "v1", "driver.py")
86-
with open(v1_lib, "w") as f:
87-
f.write(lib_template.format(1))
88-
with open(v1_driver, "w") as f:
89-
f.write(driver_template.format(gcs_address, 1))
90-
91-
os.makedirs(os.path.join(tmpdir, "v2"))
92-
v2_lib = os.path.join(tmpdir, "v2", "lib.py")
93-
v2_driver = os.path.join(tmpdir, "v2", "driver.py")
94-
with open(v2_lib, "w") as f:
95-
f.write(lib_template.format(2))
96-
with open(v2_driver, "w") as f:
97-
f.write(driver_template.format(gcs_address, 2))
106+
v1_driver = setup_driver_files(tmpdir, version=1)
107+
v2_driver = setup_driver_files(tmpdir, version=2)
98108

99109
subprocess.check_call([sys.executable, v1_driver])
100110
subprocess.check_call([sys.executable, v2_driver])

0 commit comments

Comments
 (0)