Search code examples
pythonazuredatabricksegg

Use custom functions written in Python within a Databricks notebook


I have created a python file (.py) that includes two different functions.
For example,

def function1():
    print("Hello World")

def funtion2(a, b):
    y=a+b
    if y>=5:
        print("Correct")
    else:
        print("Wrong")

Now, I want to use those two functions in a Databricks Notebook. What I did, was to follow the steps written in this databricks post. Even though I succeeded on creating an egg file that was later imported as a library in databricks I didn't manage to import my custom functions from the egg file. Please check the screenshots below:

setup.py

enter image description here

egg file on Databricks

enter image description here

When I try the python command:

import function2

I get an error that this module was not found. I appreciate any help and comments.
Note: init.py file is not used


Solution

  • I found the following solution after some searching in the Web:

      1. Create library notebook.

      For example - Lib with any functions/classes there (no runnable code).

      1. To import into Main all the classes and functions from Lib to Main use this command:
        %run "./Lib" 
        
        (this will work like: from Lib import *)
      1. After that you can call any functions / use classes that used in the Lib from Main notebook.

    This is the online post with the information.