@@ -234,6 +234,21 @@ string ListModules()
234234 return PythonEngine . Eval ( @"sorted(['%s==%s' % (i.key, i.version) for i in pkg_resources.working_set])" , null , locals . Handle ) . ToString ( ) ;
235235 }
236236
237+ void CheckImport ( string moduleName )
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 void TryToImport ( IEnumerable < string > moduleNames , string message )
238253 {
239254 List < Exception > exceptions = new List < Exception > ( ) ;
@@ -252,6 +267,7 @@ Exception TryToImport(string moduleName, string message)
252267 {
253268 try
254269 {
270+ CheckImport ( moduleName ) ;
255271 Py . Import ( moduleName ) ;
256272 return null ;
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 string folderContents = string . Join ( " " , messages ) ;
267- return new Exception ( $ "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+ string meta_path = string . Join ( Path . PathSeparator . ToString ( ) , meta_paths ) ;
285+ return new Exception ( $ "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