cancel
Showing results for 
Search instead for 
Did you mean: 
Warehousing & Analytics
Engage in discussions on data warehousing, analytics, and BI solutions within the Databricks Community. Share insights, tips, and best practices for leveraging data for informed decision-making.
cancel
Showing results for 
Search instead for 
Did you mean: 

Importing Python files into another Workspace Python file does not work

sachamourier
Contributor

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

5 REPLIES 5

filipniziol
Esteemed Contributor

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

 

 

 

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

Screenshot 2024-09-20 165613.pngScreenshot 2024-09-20 165645.png

Hi @sachamourier,
Your snapshot.py is Databricks Notebook, and not a .py file.

Check the icons.

filipniziol_0-1726845391181.png

Try adding snapshot.py as a file. It will work:

filipniziol_1-1726845462675.png

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:

filipniziol_0-1726845646839.png

 



rdokala
New Contributor III

You are absolutely right! that was not .py file.

dee_vy
New Contributor II

Hello,

I was facing the same issues as the OP and was unable to resolve them using the suggested solutions.

In my case, the root cause turned out to be the cluster configuration rather than pytest or Databricks imports.

I verified this by switching to a different cluster. The new cluster was able to see the project files correctly, imports from the src folder worked, and pytest ran successfully without any changes to the project structure.

The original cluster could see the project directories but was not resolving the file contents correctly, which resulted in ModuleNotFoundError and related import issues.

Sharing this in case it helps someone facing a similar problem. Thank you!