Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5db975a

Browse files
committed
EmbeddedPythonTest adds missing entries to sys.path
1 parent45eee51 commit5db975a

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
staticIEnumerable<string[]>MyTestCases()
2929
{
30-
yieldreturnnew[]{"test_enum","test_enum_standard_attrs"};
3130
yieldreturnnew[]{"test_generic","test_missing_generic_type"};
3231
}
3332

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

47-
// importlib gives more helpful error messages than 'import'
46+
//We could use 'import' below, butimportlib 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)

[8]ページ先頭

©2009-2025 Movatter.jp