Python API add resourcesInitScript to code env

Options
MyNamesJames
MyNamesJames Registered Posts: 5
edited July 16 in Using Dataiku

I am using the Dataiku Python API to create a code environment.

I need to add a small bit of code to the Resource Initialization script, but it seems the `code_env.get_settings()` method offers no support for doing so.

This example shows how you can use the `get_definition` approach, but the documentation states that this is deprecated and now we should use `get_settings` instead.

Here's the script I would like to add to:

import dataiku
import os

client = dataiku.api_client()

code_env = client.create_code_env(
    env_lang='python',
    env_name='<env_name>',
    deployment_mode='DESIGN_MANAGED',
    params={'pythonInterpreter': 'PYTHON311'...}
)

# load resources initialization script
resources_path = os.path.join(
    os.path.dirname(__file__),
    os.path.basename(__file__).replace('.py', '_resources.py')
)
with open(resources_path, 'rb') as f:
    resources_init_script = f.read()

settings = code_env.get_settings()

# how can I set the resourcesInitScript here?

Best Answer

  • Sarina
    Sarina Dataiker, Dataiku DSS Core Designer, Dataiku DSS Adv Designer Posts: 315 Dataiker
    edited July 17 Answer ✓
    Options

    Hi @MyNamesJames
    ,

    You can create a resources init script from the API like so:

    import dataiku
    
    client = dataiku.api_client()
    code_env = client.get_code_env('PYTHON','<CODE_ENV_NAME>')
    settings = code_env.get_settings()
    
    raw_settings = settings.get_raw()
    raw_settings['resourcesInitScript'] = 'NEW SCRIPT CONTENTS'
    
    # save the settings 
    settings.save()


    Let us know if you have any questions about this.

    Thank you,
    Sarina

Setup Info
    Tags
      Help me…