Import python script in library to code recipe
Hello,
I make a script to clean my data. The script uses data from the json files for computation purposes. I store all the script and the json in the library of my project.
So here is how the library editor looks.
I tried to import some function and class in a code recipe, but get the following error.
Is there any way I can import the function defined in the script in the library to a python recipe to make custom transformation? Or, do I have to make a plugin?
Here I attach all files that I upload to the library.
Thank you in advance for your answer.
Best Answers
-
Alexandru Dataiker, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Dataiku DSS Adv Designer, Registered Posts: 1,212 Dataiker
Hi Nurul,
Welcome to Dataiku community!When importing new libraries you just added into a Notebook you will need to restart the notebook kernel so the notebook can load these new libraries.
I see you have already created __init__.py in your subfolder. As per out doc here :
https://doc.dataiku.com/dss/latest/python/reusing-code.html
You also don't need to include the lib.python in your import.
can you try changing the after restarting the notebook Kernel and let us know if you still have issues.
from location_validation_function.synonym import batch_synonim, SynonymEntity
-
Alexandru Dataiker, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Dataiku DSS Adv Designer, Registered Posts: 1,212 Dataiker
Hi Nurul,
Indeed loading JSON files from the python library is not ideal.
You can use Managed folder instead, add the JSON files there and use the Managed folder API to read them, there are several examples here: https://doc.dataiku.com/dss/latest/connecting/managed_folders.html#usage-in-python
Depending on what you are actually storing in these json files you may want to simply define project variables instead since that is also just JSON. You can then read the variables with: via https://doc.dataiku.com/dss/latest/code_recipes/variables_expansion.html#python
Answers
-
Hi Alex,
The solution works! Thank you so much.
-
sorry, but after I can import the module, I got another error
the file is in the same folder as the .py script.
Where should I put these json files?
-
Thank you so much for your help. I managed to create and run the recipe.
Once again, thank you
-
Hi,
I am facing same issue while importing the modules we have created. I am following all the basic rules needed for import but still have the issue . can someone help on what could i be missing here.
-
This solution is not working for me, .
This is after restarting the kernel. Any advice?
-
Alexandru Dataiker, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Dataiku DSS Adv Designer, Registered Posts: 1,212 Dataiker
Hi @dlastrazeneca
,
Did you force reload the notebook, not just restart - from the menu here:If this doesn't help then please open a support ticket.
Thanks -
That does the trick!
Or using the option to "shutdown" the kernel, and then using "restart". "Restart" alone does not work.Thanks!