skip to Main Content

Getting error "Please set the default workspace with MLClient". How do I set the default workspace with MLClient? Trying to use data asset
https://learn.microsoft.com/en-us/azure/machine-learning/how-to-create-register-data-assets?tabs=Python-SDK

from azure.ai.ml.entities import Data
from azure.ai.ml.constants import AssetTypes
from azure.ai.ml import MLClient

#Enter details of your AzureML workspace
subscription_id = "<SUBSCRIPTION_ID>"
resource_group = "<RESOURCE_GROUP>"
workspace = "<AZUREML_WORKSPACE_NAME>"
ml_client = MLClient(subscription_id, resource_group, workspace)
data_location='path'

my_data = Data(
    path=data_loacation,
    type=AssetTypes.URI_FOLDER,
    description="Data",
    name="Data_test")

ml_client.data.create_or_update(my_data)

2

Answers


  1. Getting error "Please set the default workspace with MLClient". How do I set the default workspace with MLClient?

    Make sure you have installed Python SDK azure-ai-ml v2(preview) using pip install --pre azure-ai-ml

    You can try following code snippets taken from workspace.ipynb to set the default workspace with MLClient:

    Import required libraries:

    from azure.ai.ml import MLClient
    from azure.ai.ml.entities import Workspace
    from azure.identity import DefaultAzureCredential
    
    ml_client = MLClient(DefaultAzureCredential(), subscription_id, resource_group)
    

    Creating a unique workspace name with current datetime to avoid conflicts:

    import datetime
    
    basic_workspace_name = "mlw-basic-prod-" + datetime.datetime.now().strftime(
        "%Y%m%d%H%M"
    )
    
    ws_basic = Workspace(
        name=basic_workspace_name,
        location="eastus",
        display_name="Basic workspace-example",
        description="This example shows how to create a basic workspace",
        hbi_workspace=False,
        tags=dict(purpose="demo"),
    )
    ml_client.workspaces.begin_create(ws_basic)
    

    Get a list of workspaces in a resource group:

    for ws in my_ml_client.workspaces.list():
        print(ws.name, ":", ws.location, ":", ws.description)
    

    Load a specific workspace using parameters:

    ws = MLClient(DefaultAzureCredential(), subscription_id='<SUBSCRIPTION_ID>', resource_group_name='<RESOURCE_GROUP>', workspace_name='<AML_WORKSPACE_NAME>')
    
    Login or Signup to reply.
  2. I recommend to replace <SUBSCRIPTION_ID>, <RESOURCE_GROUP> and <AZUREML_WORKSPACE_NAME> with actual values.
    If you do that, the MLClient constructor will set the workspace accordingly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search