Skip to content

Commit 73a1018

Browse files
committed
Use subprocesses to avoid import conflicts
1 parent 67ae8ae commit 73a1018

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

test_run_python_code.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import importlib
1+
from subprocess import check_output, STDOUT, CalledProcessError
2+
import sys
23
import pytest
34
import glob
45

@@ -23,5 +24,18 @@ def test_run_file(file_path):
2324
if 'pytorch_fairseq_roberta' in file_path:
2425
pytest.skip("temporarily disabled")
2526

26-
module = file_path[:-3].replace('/', '.') # :-3 to remove the .py extension
27-
importlib.import_module(module)
27+
# We just run the python files in a separate sub-process. We really want a
28+
# subprocess here because otherwise we might run into package versions
29+
# issues: imagine script A that needs torchvivion 0.9 and script B that
30+
# needs torchvision 0.10. If script A is run prior to script B in the same
31+
# process, script B will still be run with torchvision 0.9 because the only
32+
# "import torchvision" statement that counts is the first one, and even
33+
# torchub sys.path shenanigans can do nothing about this. By creating
34+
# subprocesses we're sure that all file executions are fully independent.
35+
try:
36+
# This is inspired (and heavily simplified) from
37+
# https://github.com/cloudpipe/cloudpickle/blob/343da119685f622da2d1658ef7b3e2516a01817f/tests/testutils.py#L177
38+
out = check_output([sys.executable, file_path], stderr=STDOUT)
39+
print(out.decode())
40+
except CalledProcessError as e:
41+
raise RuntimeError(f"Script {file_path} errored with output:\n{e.output.decode()}")

0 commit comments

Comments
 (0)