8000 EmbeddedPythonTest adds missing entries to sys.path · pythonnet/pythonnet@5db975a · GitHub
[go: up one dir, main page]

Skip to content

Commit 5db975a

Browse files
committed
EmbeddedPythonTest adds missing entries to sys.path
1 parent 45eee51 commit 5db975a

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/embed_tests/TestPythonTests.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public void Dispose()
2727

2828
static IEnumerable<string[]> MyTestCases()
2929
{
30-
yield return new[] { "test_enum", "test_enum_standard_attrs" };
3130
yield return new[] { "test_generic", "test_missing_generic_type" };
3231
}
3332

@@ -44,18 +43,21 @@ public void EmbeddedPythonTest(string testFile, string testName)
4443
string path = Path.Combine(folder, testFile + ".py");
4544
if (!File.Exists(path)) throw new FileNotFoundException("Cannot find test file", path);
4645

47-
// importlib gives more helpful error messages than 'import'
46+
// We could use 'import' below, but importlib gives more helpful error messages than 'import'
4847
// https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
48+
// Sometimes sys.path does not contain the site-packages directory, so we add it explicitly
49+
// Because the Python tests sometimes have relative imports, the module name must be inside the tests package
4950
PythonEngine.Exec($@"
5051
import sys
5152
import os
52-
fixtures_path = os.path.join(r'{folder}', 'fixtures')
53-
sys.path.append(fixtures_path)
53+
libdir = os.path.dirname(sys.modules['os'].__file__)
54+
sys.path.append(os.path.join(libdir, 'site-packages'))
55+
sys.path.append(os.path.dirname(r'{folder}'))
56+
sys.path.append(os.path.join(r'{folder}', 'fixtures'))
5457
import clr
5558
clr.AddReference('Python.Test')
56-
sys.path.append(r'{folder}')
57-
import utils
58-
module_name = '{testFile}'
59+
import tests
60+
module_name = 'tests.{testFile}'
5961
file_path = r'{path}'
6062
import importlib.util
6163
spec = importlib.util.spec_from_file_location(module_name, file_path)

0 commit comments

Comments
 (0)
0