Import Python files into another python file of workspace in Azure databricks

tech kamar
Jun 12, 2024

Imagine you created 2 python files namely main.py and greeter.py and you uploaded both in Azure DataBricks workspace inside same directory.

Your main.py looks like this

import greeter
greeter.goodmorning()

Your greeter.py looks like this

def goodmorning():
print("Good Morning")

Now you try running main.py and you will get this error

Module is not found

How do you solve this Import problem and make your code work?

Solution: Append path of your folder to sys.path

So I am adding these lines to main.py

both main.py and greeter.py are present inside testing folder inside Shared part of workspace.

import os
import sys
sys.path.append("/Workspace/Shared/testing/")

Now main.py looks like this

import os
import sys
sys.path.append("/Workspace/Shared/testing/")
import greeter
greeter.goodmorning()

Now let’s try running main.py

output is coming now. No more module import error

If you this tutorial helped you. Please give a clap :)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response