Survey banner
Switching to Dataiku - a new area to help users who are transitioning from other tools and diving into Dataiku! CHECK IT OUT

Python API add resourcesInitScript to code env

Solved!
MyNamesJames
Level 2
Python API add resourcesInitScript to code env

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?
0 Kudos
1 Solution
SarinaS
Dataiker

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 

View solution in original post

0 Kudos
1 Reply
SarinaS
Dataiker

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 

0 Kudos