@@ -228,10 +228,17 @@ public void SetPythonPath()
228
228
229
229
string ListModules ( )
230
230
{
231
- var pkg_resources = Py . Import ( "pkg_resources" ) ;
232
- var locals = new PyDict ( ) ;
233
- locals . SetItem ( "pkg_resources" , pkg_resources ) ;
234
- return PythonEngine . Eval ( @"sorted(['%s==%s' % (i.key, i.version) for i in pkg_resources.working_set])" , null , locals . Handle ) . ToString ( ) ;
231
+ try
232
+ {
233
+ var pkg_resources = Py . Import ( "pkg_resources" ) ;
234
+ var locals = new PyDict ( ) ;
235
+ locals . SetItem ( "pkg_resources" , pkg_resources ) ;
236
+ return PythonEngine . Eval ( @"sorted(['%s==%s' % (i.key, i.version) for i in pkg_resources.working_set])" , null , locals . Handle ) . ToString ( ) ;
237
+ }
238
+ catch ( PythonException ex )
239
+ {
240
+ return ex . ToString ( ) ;
241
+ }
235
242
}
236
243
237
244
void CheckImport ( string moduleName )
@@ -240,12 +247,13 @@ void CheckImport(string moduleName)
240
247
module_name = r'{ moduleName } '
241
248
import sys
242
249
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)
250
+ if module_name not in sys.modules:
251
+ spec = importlib.util.find_spec(module_name)
252
+ if spec is None:
253
+ raise ImportError('find_spec returned None')
254
+ module = importlib.util.module_from_spec(spec)
255
+ sys.modules[module_name] = module
256
+ spec.loader.exec_module(module)
249
257
" ) ;
250
258
}
251
259
@@ -254,7 +262,7 @@ void TryToImport(IEnumerable<string> moduleNames, string message)
254
262
List < Exception > exceptions = new List < Exception > ( ) ;
255
263
foreach ( var moduleName in moduleNames )
256
264
{
257
- var exception = TryToImport ( moduleName , " before setting PythonPath" ) ;
265
+ var exception = TryToImport ( moduleName , message ) ;
258
266
if ( exception != null ) exceptions . Add ( exception ) ;
259
267
}
260
268
if ( exceptions . Count > 0 )
@@ -280,9 +288,7 @@ Exception TryToImport(string moduleName, string message)
280
288
$ " { folder } contains { string . Join ( Path . PathSeparator . ToString ( ) , Directory . EnumerateFileSystemEntries ( folder ) . Select ( fullName => Path . GetFileName ( fullName ) ) . ToArray ( ) ) } " :
281
289
"" ) . ToArray ( ) ;
282
290
string folderContents = string . Join ( " " , messages ) ;
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 ) ;
291
+ return new Exception ( $ "Py.Import(\" { moduleName } \" ) failed { message } , sys.path={ path } { folderContents } pkg_resources.working_set={ ListModules ( ) } ", ex ) ;
286
292
}
287
293
}
288
294
}
0 commit comments