-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(component): execute in a virtual env (#11326)
This commit introduces the ability to run the component inside a virtual environment, which is particularly useful when the Python system site package directory is read-only, causing pip to fail during the installation of kfp and other dependencies. To bypass this issue, a writable temporary directory is selected to create a virtual environment that inherits the global system site packages. The entire workflow is then executed from within this virtual environment. Furthermore, a new field, `use_venv`, has been added to the component decorator, with a default value of `False`. Signed-off-by: Sébastien Han <[email protected]>
- Loading branch information
Showing
5 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
sdk/python/test_data/components/component_with_pip_install_in_venv.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright 2024 The Kubeflow Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from kfp.dsl import component | ||
|
||
|
||
@component( | ||
pip_index_urls=["https://pypi.org/simple"], | ||
packages_to_install=["yapf"], | ||
use_venv=True, | ||
) | ||
def component_with_pip_install(): | ||
import yapf | ||
|
||
print(dir(yapf)) | ||
|
||
|
||
if __name__ == "__main__": | ||
from kfp import compiler | ||
|
||
compiler.Compiler().compile( | ||
pipeline_func=component_with_pip_install, | ||
package_path=__file__.replace(".py", ".yaml"), | ||
) |
55 changes: 55 additions & 0 deletions
55
sdk/python/test_data/components/component_with_pip_install_in_venv.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# PIPELINE DEFINITION | ||
# Name: component-with-pip-install | ||
components: | ||
comp-component-with-pip-install: | ||
executorLabel: exec-component-with-pip-install | ||
deploymentSpec: | ||
executors: | ||
exec-component-with-pip-install: | ||
container: | ||
args: | ||
- --executor_input | ||
- '{{$}}' | ||
- --function_to_execute | ||
- component_with_pip_install | ||
command: | ||
- sh | ||
- -c | ||
- "\nif ! [ -x \"$(command -v pip)\" ]; then\n python3 -m ensurepip ||\ | ||
\ python3 -m ensurepip --user || apt-get install python3-pip\nfi\n\nPIP_DISABLE_PIP_VERSION_CHECK=1\ | ||
\ \nexport PIP_DISABLE_PIP_VERSION_CHECK=1\ntmp=$(mktemp -d)\npython3 -m\ | ||
\ venv \"$tmp/venv\" --system-site-packages\n. \"$tmp/venv/bin/activate\"\ | ||
\n python3 -m pip install --quiet --no-warn-script-location --index-url\ | ||
\ https://pypi.org/simple --trusted-host https://pypi.org/simple 'kfp==2.9.0'\ | ||
\ '--no-deps' 'typing-extensions>=3.7.4,<5; python_version<\"3.9\"' &&\ | ||
\ python3 -m pip install --quiet --no-warn-script-location --index-url\ | ||
\ https://pypi.org/simple --trusted-host https://pypi.org/simple 'yapf'\ | ||
\ && \"$0\" \"$@\"\n" | ||
- sh | ||
- -ec | ||
- 'program_path=$(mktemp -d) | ||
printf "%s" "$0" > "$program_path/ephemeral_component.py" | ||
_KFP_RUNTIME=true python3 -m kfp.dsl.executor_main --component_module_path "$program_path/ephemeral_component.py" "$@" | ||
' | ||
- "\nimport kfp\nfrom kfp import dsl\nfrom kfp.dsl import *\nfrom typing import\ | ||
\ *\n\ndef component_with_pip_install():\n import yapf\n\n print(dir(yapf))\n\ | ||
\n" | ||
image: python:3.9 | ||
pipelineInfo: | ||
name: component-with-pip-install | ||
root: | ||
dag: | ||
tasks: | ||
component-with-pip-install: | ||
cachingOptions: | ||
enableCache: true | ||
componentRef: | ||
name: comp-component-with-pip-install | ||
taskInfo: | ||
name: component-with-pip-install | ||
schemaVersion: 2.1.0 | ||
sdkVersion: kfp-2.9.0 |