Importing Python files into another Workspace Python file does not work
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2024 07:27 AM
I have created Python modules containing some Python functions and I would like to import them from a notebook contained in the Workspace.
For example, I have a "etl" directory, containing a "snapshot.py" file with some Python functions, and an empty "__init__.py" file as well. When working inside my repo in Databricks, the "from etl.snapshot import *" Python command works with no issue and I am able to call my functions. However, when running that same command from the Workspace, I get a "Module not found" error. I have tried to use the "sys.path.append()" approach as suggested in many forums or in multiple YouTube videos but this still does not work.
I am looking for a solution, as I would like to avoid using the %run command.
Thanks a lot in advance for your help,
Sacha
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2024 07:45 AM
Hi @sachamourier ,
It will work, but you need carefully craft path to sys.path.append(), you even do not need __init__.py to make it work.
Try to hard-code the path to the snapshot.py in workspace.
Add this to your notebook:
import sys import os absolute_directory_path = os.path.normpath("/Workspace/<path to directory where the snapshot.py file is located>") if absolute_directory_path not in sys.path: sys.path.append(absolute_directory_path)
And then you make your import:
import snapshot
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2024 07:57 AM - edited 09-20-2024 07:58 AM
Hi @filipniziol ,
Thank you for your response. I have tried but this still does not work. The path gets added to "sys.path" though. You can find attached images of the issue.
Sacha
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2024 08:18 AM - edited 09-20-2024 08:20 AM
Hi @sachamourier,
Your snapshot.py is Databricks Notebook, and not a .py file.
Check the icons.
Try adding snapshot.py as a file. It will work:
Also, after you replace snapshot notebook with snapshot file, make sure to clear state in the notebook that you use to reference the snapshot.py:

