Shubh
Shubh

Reputation: 593

How to import a module into another module in databricks notebook?

This is my config.py in Databricks

DATA_S3_LOCATION='s3://server-data/data1'
DATA_S3_FILE_TYPE='orc'
DATA2_S3_LOCATION='s3://server-data/data2'
DATA2_S3_FILE_TYPE='orc'

I have init . py in this folder as well

I am trying to access these variables in another file

import sys
sys.path.insert(1,'/Users/file')
from file import config

I am facing error , no module named file

Upvotes: 3

Views: 7927

Answers (1)

Alex Ott
Alex Ott

Reputation: 87279

There are several aspects here.

  • If these files are notebooks, then you need to use %run ./config to include notebook from the current directory (doc)
  • if you're using Databricks Repos and arbitrary files support is enabled, then your code needs to be a Python file, not notebook, and have correct directory layout with __init__.py, etc. In this case, you can use Python imports. Your repository directory will be automatically added into a sys.path and you don't need to modify it.

P.S. I have an example of repository with both notebooks & Python files approaches.

Upvotes: 5

Related Questions