Search code examples
pythonazuremachine-learningcomponentsazureml-python-sdk

Load Registered Component in Azure ML for Pipeline using Python sdk v2


I'm working in Azure Machine Learning Studio to create components that I will run together in a pipeline. In this basic example, I have a single python script and a single yml file that make up my component, along with a notebook I am using to define, instantiate and run a pipeline. See an overview of the folder structure I have below for this component.

📦component
 ┣ 📜notebook.ipynb
 ┣ 📜component_script.py
 ┗ 📜component_def.yml

Inside my notebook I can then load the component and register it to the workspace using the code below (note that here I have already instantiated my ml_client object).

# importing the Component Package
from azure.ai.ml import load_component

# Loading the component from the yml file
component = load_component("component_def.yml")

# Now we register the component to the workspace
component = ml_client.create_or_update(component)

I can then pass this component into a pipeline successfully. My question is, now that I have registered my component, I should no longer need to instantiate my component object using component = load_component("component_def.yml") which requires access to the yml file. I should instead be able to instantiate my component object from the registered component. How can I do this?


Solution

  • I've found the solution in the microsoft documentation, posting the answer here in case anyone else is struggling to find the solution. Turns out this is quite straightforward to achieve using the python sdk v2, it can be done in a single line of code, see an extract from the docs here below:

    comp = ml_client.components.get(name="<component_name>", version="<component_version>")