Importing code from a plugin into python notebook

Antal
Antal Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Neuron, Dataiku DSS Adv Designer, Registered, Neuron 2023 Posts: 91 Neuron

I'd like to import python functions from a (self-made) plugin into a python notebook.

I've found this part in the User's Guide, but it's lacking examples and I can't get it to work:

https://doc.dataiku.com/dss/latest/plugins/reference/other.html

If I have a plugin with plugin id "xyz" and the following folder structure

xyz

----python-lib

--------custom_functions

--------------function_script.py

--------------__init__.py

How do I then use the functions dataiku.use_plugin_lib(pluginid) and dataiku.import_from_plugin(pluginid,packagename) to import functions from function_script.py above?

Let's say function_script.py contains 2 functions that I both want to import.


Operating system used: Windows

Best Answer

  • JordanB
    JordanB Dataiker, Dataiku DSS Core Designer, Dataiku DSS Adv Designer, Registered Posts: 296 Dataiker
    Answer ✓

    Hi @Antal
    ,

    Can you try putting the function_script.py file in the python-lib folder instead of the sub-folder? Also, was this code imported from Git? If so, the folder has most likely not been added to your PATH and will need to be added. Lastly, could you please double-check the permissions of your python-lib directory and all parent directories?

    Thanks!

    Jordan

Answers

  • JordanB
    JordanB Dataiker, Dataiku DSS Core Designer, Dataiku DSS Adv Designer, Registered Posts: 296 Dataiker

    Hi @Antal
    ,

    Once you import the package using the DSS function dataiku.import_from_plugin, you should be able to use an import statement to access the functions. For example, "from function_script import *"

    If you are receiving errors, please share a screenshot.

    Thanks!

    Jordan

  • Antal
    Antal Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Neuron, Dataiku DSS Adv Designer, Registered, Neuron 2023 Posts: 91 Neuron
    edited July 17

    If I try

    import dataiku
    dataiku.import_from_plugin('xyz', 'function_script')
    from function_script import num_qt

    I get an error as in the screenshot

    Using

    import dataiku
    dataiku.import_from_plugin('xyz', 'function_script.py')
    from function_script import num_qt

    instead gives the same error

    (nb in the screenshot, the pluginid name is deleted for confidentiality purposes and the script name is diff_functions.py instead)

  • Antal
    Antal Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Neuron, Dataiku DSS Adv Designer, Registered, Neuron 2023 Posts: 91 Neuron

    Yes, when I put the script in the root of python-lib I can load from the plugin lib as expected.

    I put them in a subdir for a reason, though. For clarity in plugin organisation.

    It would be convenient if we could also load the code from subdirs in a Plugins python-lib

  • JordanB
    JordanB Dataiker, Dataiku DSS Core Designer, Dataiku DSS Adv Designer, Registered Posts: 296 Dataiker

    Hi @Antal

    You should be able to load the code from a subdirectory in python-lib, I just wanted to make sure that you had permission to read from that path. Can you please try the subdirectory again, SSH into the dss host and make sure the permissions are ok on that subdir? Also, please make sure that your __init__.py file contains def num_qt. And, try from custom_functions import function_script and/or from custom_functions.function_script import num_qt.

    Thanks!

    Jordan

Setup Info
    Tags
      Help me…