%run magic using get_ipython().run_line_magic() in Databricks

Viewed 697

I am trying to import other modules inside an Azure Databricks notebook. For instance, I want to import the module called 'mynbk.py' that is at the same level as my current Databricks notebook called 'myfile'

To do so, inside 'myfile', in a cell, I use the magic command:

%run ./mynbk

And that works fine.

Now, I would like to achieve the same result, but with using get_ipython().run_line_magic()

I thought, this is what I needed to type:

get_ipython().run_line_magic('run', './mynbk')

Unfortunately, that does not work. The error I get is:

Exception: File `'./mynbk.py'` not found.

Any help is appreciated.

1 Answers

It won't work on Databricks because IPython commands doesn't know about Databricks-specific implementation, and IPython's %run is expecting the file to execute, but Databricks notebooks aren't files on the disk, but the data stored in the database, so %run from IPython can't find it, and you get error.

Related