Skip to content

Commit f87a730

Browse files
pierrot0The TensorFlow Datasets Authors
authored andcommitted
when dynamically loading a module defining a Builder, load from within package so name is correctly inferred using package name if the module doesn't follow naming recommendations.
PiperOrigin-RevId: 502530611
1 parent d88e087 commit f87a730

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

tensorflow_datasets/scripts/cli/build.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,19 @@ def _get_builder_cls(
344344
if path is not None:
345345
logging.info(f'Loading dataset {ds_to_build} from path: {path}')
346346
# Dynamically load user dataset script
347+
# When possible, load from the parent's parent, so module is named
348+
# "foo.foo_dataset_builder".
349+
try:
350+
with tfds.core.utils.add_sys_path(path.parent.parent):
351+
builder_cls = tfds.core.community.builder_cls_from_module(
352+
f'{path.parent.stem}.{path.stem}'
353+
)
354+
return builder_cls, {}
355+
except ImportError:
356+
pass
357+
# There might be cases where user imports from a legacy builder module,
358+
# in which case the above wouldn't necessarily work, so we fall back to
359+
# importing the module directly.
347360
with tfds.core.utils.add_sys_path(path.parent):
348361
builder_cls = tfds.core.community.builder_cls_from_module(path.stem)
349362
return builder_cls, {}

0 commit comments

Comments
 (0)