05-06-2024 07:18 AM
Hello,
Some variations of this question have been asked before but there doesn't seem to be an answer for the following simple use case:
I have the following file structure on a Databricks Asset Bundles project:
src
--dir1
----file1.py
--dir2
----file2.py
There doesn't seem to be a way to import anything from file1.py to file2.py. I have tried (inside file2.py):
from src.dir1.file1 import something
from dir1.file1 import something
from ..dir1.file1 import something
None of these seem to work. I have tried appending os.abspath("..") and "../.." to sys.path, as well as passing ${workspace.file_path} to the task and appending that, to no avail.
Note that relative imports on the same level work so the following both work:
# src
# --file1.py
# --file2.py
# We are on file1.py
from file2 import something
# src
# --file1.py
# --dir2
# ----file2.py
# We are on file1.py
from dir2.file2 import something
Is something weird going on with the import paths when deploying DABs? I'd rather avoid having to deploy a full-on wheel if I can avoid it, as it adds a lot of unnecessary boilerplate.
Thank you!
08-02-2024 06:57 AM
Unfortunately none of the methods proposed here worked, so we ended up having to wrap our code in packages and installing those in the cluster before running. Not an ideal workflow, but works for projects that are easily decoupled from their orchestration.
05-06-2024 07:57 AM
Hi @georgef, It appears that you’re encountering issues with importing modules within a Databricks Asset Bundles (DABs) project.
Let’s explore some potential solutions to address this problem.
Bundle Deployment and Import Paths:
Explicitly Add the Source Directory to sys.path:
file2.py
, you can explicitly add the source directory (src
) to the Python path using sys.path.append()
.file2.py
as follows:import sys
sys.path.append('/Workspace/${workspace.file_path}/src')
from src.dir1.file1 import something
${workspace.file_path}
with the actual path to your workspace. This approach ensures that the src
directory is included in the Python path.Use Parameters in Databricks YAML:
databricks.yml
file, pass an argument to your script via parameters.databricks.yml
as follows:resources:
jobs:
my_job:
name: my_job
tasks:
- task_key: my_task
existing_cluster_id: YYY
spark_python_task:
python_file: src/jobs/bronze/my_script.py
parameters: ['/Workspace/${workspace.file_path}/src']
my_script.py
, retrieve the parameter and add it to the Python path:import sys
bundle_src_path = sys.argv[1]
sys.path.append(bundle_src_path)
from src.jobs.common import *
${workspace.file_path}
with the actual path to your workspace.Other Approaches:
%run
with inline code in a notebook.Remember to adjust the paths and configurations according to your specific setup. DABs can be powerful for packaging and distributing code, but understanding their behaviour is crucial for successful deployment.
Good luck! 😊
08-02-2024 06:57 AM
Unfortunately none of the methods proposed here worked, so we ended up having to wrap our code in packages and installing those in the cluster before running. Not an ideal workflow, but works for projects that are easily decoupled from their orchestration.
08-02-2024 07:11 AM
Hi. This was a long-standing issue for me too. This solution may not be what is desired, but it works perfectly for my needs.
In my python code, I have this structure:
if __name__ == '__main__':
# directory structure where "mycode" is this code here, trying to import "mymodule"
# > main
# -- mymodule
# > subdir
# -- mycode
# Get the current directory
current_directory = os.getcwd()
# Get the parent directory
parent_directory = os.path.abspath(os.path.join(current_directory, '..'))
# Add the parent directory to sys.path
sys.path.append(parent_directory)
from mymodule import myfunction
Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.
If there isn’t a group near you, start one and help create a community that brings people together.
Request a New Group