- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2023 10:52 AM
@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:
- Move the .py files containing the functions you want to import to the Workspace/Shared folder.
- 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.
- 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_functionNote 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.