Skip to content

Commit

Permalink
fix(sdk): fixes module not found error for containerized python compo…
Browse files Browse the repository at this point in the history
…nents. Fixes kubeflow#8385 (kubeflow#9157)

* Add module directory to sys.path

* Add nested module imports unit test

* Add release note to release.md
  • Loading branch information
Mithil467 authored and rd-pong committed Apr 26, 2023
1 parent 953c96e commit 4c46628
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions sdk/RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
## Deprecations

## Bug fixes and other changes
* Fix module not found error for containerized python components [\#9157](https://github.com/kubeflow/pipelines/pull/9157)

## Documentation updates

Expand Down
18 changes: 18 additions & 0 deletions sdk/python/kfp/cli/component_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,24 @@ def test_component_filepattern_can_be_used_to_restrict_discovery(self):
'''))

def test_nested_module_imports(self):
module_two = 'two = 2'
_write_file('module_two.py', module_two)

module_one = 'from module_two import two\none = 1'
_write_file('module_one.py', module_one)

component = _make_component(
func_name='comp', target_image='custom-image')
component = 'from module_one import one\n' + component
_write_components('component.py', component)

result = self.runner.invoke(
self.cli,
['build', str(self._working_dir)],
)
self.assertEqual(result.exit_code, 0)

def test_emptry_requirements_txt_file_is_generated(self):
component = _make_component(
func_name='train', target_image='custom-image')
Expand Down
1 change: 1 addition & 0 deletions sdk/python/kfp/components/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def load_module(module_name: str, module_directory: str) -> types.ModuleType:
location=os.path.join(module_directory, f'{module_name}.py'))
module = importlib.util.module_from_spec(module_spec)
sys.modules[module_spec.name] = module
sys.path.insert(0, str(module_directory))
module_spec.loader.exec_module(module)
return module

Expand Down

0 comments on commit 4c46628

Please sign in to comment.