简体   繁体   中英

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

在此处输入图像描述

egg file on Databricks

在此处输入图像描述

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM