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

Commitff38409

Browse files
committed
SetPythonPath displays more information on failure
1 parent25d2354 commitff38409

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

‎src/embed_tests/TestPythonEngineProperties.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,21 @@ string ListModules()
234234
returnPythonEngine.Eval(@"sorted(['%s==%s' % (i.key, i.version) for i in pkg_resources.working_set])",null,locals.Handle).ToString();
235235
}
236236

237+
voidCheckImport(stringmoduleName)
238+
{
239+
PythonEngine.Exec($@"
240+
module_name = r'{moduleName}'
241+
import sys
242+
import importlib.util
243+
spec = importlib.util.find_spec(module_name)
244+
if spec is None:
245+
raise ImportError('find_spec returned None')
246+
module = importlib.util.module_from_spec(spec)
247+
sys.modules[module_name] = module
248+
spec.loader.exec_module(module)
249+
");
250+
}
251+
237252
voidTryToImport(IEnumerable<string>moduleNames,stringmessage)
238253
{
239254
List<Exception>exceptions=newList<Exception>();
@@ -252,6 +267,7 @@ Exception TryToImport(string moduleName, string message)
252267
{
253268
try
254269
{
270+
CheckImport(moduleName);
255271
Py.Import(moduleName);
256272
returnnull;
257273
}
@@ -264,7 +280,9 @@ Exception TryToImport(string moduleName, string message)
264280
$"{folder} contains{string.Join(Path.PathSeparator.ToString(),Directory.EnumerateFileSystemEntries(folder).Select(fullName=>Path.GetFileName(fullName)).ToArray())}":
265281
"").ToArray();
266282
stringfolderContents=string.Join(" ",messages);
267-
returnnewException($"Py.Import(\"{moduleName}\") failed{message}, sys.path={path}{folderContents} pkg_resources.working_set={ListModules()}",ex);
283+
object[]meta_paths=Py.Import("sys").GetAttr("meta_path").As<object[]>();
284+
stringmeta_path=string.Join(Path.PathSeparator.ToString(),meta_paths);
285+
returnnewException($"Py.Import(\"{moduleName}\") failed{message}, sys.path={path}{folderContents} pkg_resources.working_set={ListModules()} sys.meta_path={meta_path}",ex);
268286
}
269287
}
270288
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp