Anonymous
Not applicable

@Christine Pedersen​ :

Yes, it is possible to create libraries for your Databricks notebooks and import functions from modules saved in repos, as well as from modules stored in the Workspace/Shared directory. However, you will need to take a few extra steps to properly import the modules stored in the Shared directory.

Here are the steps to follow:

  1. Move the .py files containing the functions you want to import to the Workspace/Shared folder.
  2. Create an empty file called __init__.py in the same directory as your .py files. This is necessary to make Python recognize the directory as a package.
  3. In your notebook, add the following code at the beginning to add the Workspace/Shared folder to the Python path:
import sys
sys.path.append("/Workspace/Shared")

4) To import functions from a module, use the following syntax:

from <folder_name>.<module_name> import <function_name>

For example, if your folder name is my_lib and your module name is my_module, and you want to import the function my_function, you would use:

from my_lib.my_module import my_function

Note that you should omit the .py extension when specifying the module name.

By following these steps, you should be able to import functions from modules stored in the Workspace/Shared directory as if they were in a library.

View solution in original post