I’m trying to import a module using Python files in Databricks, but it’s not a notebook and not Spark. Python version 3.10.12.
In the Databricks workspace (Git repo, not user workspace), I define this in testmodule.py:
def sum_numbers(a, b):
return a + b
class Circle:
def __init__(self, radius):
self.radius = radius
self.color = 'black'
def calculate_area(self):
return round(math.pi * self.radius ** 2, 2)
In another Python file, I run
import testmodule as tm
tm.sum_numbers(4,20)
which correctly gives the output of 24. So why can’t I do this?
from testmodule import Circle
ImportError: cannot import name 'Circle' from 'testmodule' (/Workspace/Repos/[email protected]/myGitRepo/testmodule.py)
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
File <command-123>, line 12
11 tm.sum_numbers(4,20)
---> 12 from testmodule import Circle
FYI, I’m aware how to import a notebook like this, but I need to be able to run the code in other environments that may not be Databricks or Spark.
%run ./testmodule