Containerized Python Components
Create Python components with more complex dependencies
Last updated
Create Python components with more complex dependencies
Last updated
The following assumes a basic familiarity with .
Containerized Python Components extend by relaxing the constraint that Lightweight Python Components be hermetic (i.e., fully self-contained). This means Containerized Python Component functions can depend on symbols defined outside of the function, imports outside of the function, code in adjacent Python modules, etc. To achieve this, the KFP SDK provides a convenient way to package your Python code into a container.
The following shows how to use Containerized Python Components by modifying the add
component from the example:
Start by creating an empty src/
directory to contain your source code:
Next, add the following simple module, src/math_utils.py
, with one helper function:
Lastly, move your component to src/my_component.py
and modify it to use the helper function:
src
now looks like this:
In this step, you’ll provide base_image
and target_image
arguments to the @dsl.component
decorator of your component in src/my_component.py
:
The previous example includes base_image
for clarity, but this is not necessary as base_image
will default to 'python:3.7'
if omitted.
Finally, you can use the component in a pipeline:
Setting target_image
both (a) specifies the for the image you’ll build in Step 3, and (b) instructs KFP to run the decorated Python function in a container that uses the image with that tag.
In a Containerized Python Component, base_image
specifies the base image that KFP will use when building your new container image. Specifically, KFP uses the base_image
argument for the instruction in the Dockerfile used to build your image.
Now that your code is in a standalone directory and you’ve specified a target image, you can conveniently build an image using the CLI command:
If you have a , you can replace the --no-push-image
flag with --push-image
to automatically push the image after building.
Since add
’s target_image
uses (indicated by the gcr.io
URI), the pipeline shown here assumes you have pushed your image to Google Cloud Artifact Registry, you are running your pipeline on , and you have configured so that Vertex AI Pipelines can pull images from Artifact Registry.