@@ -305,16 +305,23 @@ def store_metadata_file(metadata_filename: str):
305
305
306
306
def import_class_from_file (file_path : Path , class_name ):
307
307
"""Import a class from a file."""
308
- module_name = file_path .stem
309
- spec = spec_from_file_location (module_name , file_path )
310
- if spec is None :
311
- raise ImportError (f"Could not load spec from { file_path } " )
312
-
313
- # create a module from the spec and execute it
314
- module = module_from_spec (spec )
315
- spec .loader .exec_module (module )
316
- if not hasattr (module , class_name ):
317
- raise ImportError (f"Module '{ module_name } ' has no class '{ class_name } '" )
308
+
309
+ def load_module (module_name ):
310
+ spec = spec_from_file_location (module_name , file_path )
311
+ if spec is None :
312
+ raise ImportError (f"Could not load spec from { file_path } " )
313
+
314
+ # create a module from the spec and execute it
315
+ module = module_from_spec (spec )
316
+ spec .loader .exec_module (module )
317
+ if not hasattr (module , class_name ):
318
+ raise ImportError (f"Module '{ module_name } ' has no class '{ class_name } '" )
319
+ return module
320
+
321
+ try :
322
+ module = load_module (file_path .stem )
323
+ except ImportError :
324
+ module = load_module (f"{ file_path .parent .stem } .{ file_path .stem } " )
318
325
319
326
# return the class from the module
320
327
return getattr (module , class_name )
0 commit comments