@@ -234,6 +234,21 @@ string ListModules()
234
234
return PythonEngine . Eval ( @"sorted(['%s==%s' % (i.key, i.version) for i in pkg_resources.working_set])" , null , locals . Handle ) . ToString ( ) ;
235
235
}
236
236
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
+
237
252
void TryToImport ( IEnumerable < string > moduleNames , string message )
238
253
{
239
254
List < Exception > exceptions = new List < Exception > ( ) ;
@@ -252,6 +267,7 @@ Exception TryToImport(string moduleName, string message)
252
267
{
253
268
try
254
269
{
270
+ CheckImport ( moduleName ) ;
255
271
Py . Import ( moduleName ) ;
256
272
return null ;
257
273
}
@@ -264,7 +280,9 @@ Exception TryToImport(string moduleName, string message)
264
280
$ "{ folder } contains{ string . Join ( Path . PathSeparator . ToString ( ) , Directory . EnumerateFileSystemEntries ( folder ) . Select ( fullName=> Path . GetFileName ( fullName ) ) . ToArray ( ) ) } ":
265
281
"" ) . ToArray ( ) ;
266
282
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 ) ;
268
286
}
269
287
}
270
288
}