3

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

2
  • Try "from python_functions.py import function2" or try " import python_function.py" then use function2. Commented May 5, 2020 at 8:19
  • @ZubadIbrahim Both ways you have written already gave me "No module named 'python_functions'". That's why I tried the .egg file approach. Commented May 5, 2020 at 8:40

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.