cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

code execution from Databrick folder

Kartikb
New Contributor II

We are able to run a notebook that references Python code using import statements from a Databricks repo
with the source code checked out. However, we encounter a ModuleNotFoundError when executing the same code from a folder.

Error: ModuleNotFoundError: No module named ...

Is there a future roadmap to enable code execution from Databricks folder and subfolders?

1 ACCEPTED SOLUTION

Accepted Solutions

saurabh18cs
Contributor II

Hi Kartik,

Can you elaborate further on your query? I may not able to understand you properly but if your source code is checked out to databricks repos and you want to execute your code for testing purposes , had you try setting up your repo dir into sys paths?

 

this can go into the top cell of your execution notebook before you're doing your imports.

import os, sys
# get current directory
path = os.getcwd()
print("Current Directory", path)
# prints parent directory
repo_dir = os.path.abspath(os.path.join(path, os.pardir))
# repo_dir = os.path.dirname(os.pardir())
sys.path.append(f"{repo_dir}/")
print(repo_dir)

View solution in original post

4 REPLIES 4

saurabh18cs
Contributor II

Hi Kartik,

Can you elaborate further on your query? I may not able to understand you properly but if your source code is checked out to databricks repos and you want to execute your code for testing purposes , had you try setting up your repo dir into sys paths?

 

this can go into the top cell of your execution notebook before you're doing your imports.

import os, sys
# get current directory
path = os.getcwd()
print("Current Directory", path)
# prints parent directory
repo_dir = os.path.abspath(os.path.join(path, os.pardir))
# repo_dir = os.path.dirname(os.pardir())
sys.path.append(f"{repo_dir}/")
print(repo_dir)

filipniziol
Contributor III

Hi @Kartikb ,

This feature is already available. 

As @saurabh18cs  mentioned, you probably have not added the location of your python code to sys.path.

Also, it is important to note, that your python code must be a file, and not a notebook. You can recognize by checking the object icon or object type:

filipniziol_0-1729505797970.png

filipniziol_1-1729505930222.png

 



 

Panda
Valued Contributor

@Kartikb Best approach is by creating a package and Install Code as a Module or Wheel. This ensures your code is accessible across notebooks and distributed jobs without manual path adjustmentsโ€‹

Kartikb
New Contributor II
Below worked as you have suggested.
import os, sys
project_path = os.path.abspath("/Workspace/<folder-name-1>/<folder-name-2>/<top-level-code-folder>")
if project_path not in sys.path:
    sys.path.append(project_path)
 

Connect with Databricks Users in Your Area

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