databricks-vscode icon indicating copy to clipboard operation
databricks-vscode copied to clipboard

[BUG] %run commands fail to run even after latest release

Open TumbleTuesday opened this issue 1 year ago • 9 comments

Describe the bug I noticed that #1611 was resolved by #1612 however after updating the extension and trying again it still seems to fail. In addition, if the cell has a title it now gives an additional error.

To Reproduce Steps to reproduce the behaviours

With dbtitle

  1. run a notebook using a local path and a dbtitle like so:
# COMMAND ----------

# DBTITLE 1,Run the Functions
# MAGIC %run "../../the_functions"

  1. receive error:

UserWarning: Could not open file <C:\Users\username\AppData\Local\Temp\tmpmd0gf5yo.ipynb> for safe execution. warn('Could not open file <%s> for safe execution.' % fname)

Without dbtitle

  1. run a notebook using a local path like so:
# COMMAND ----------

# MAGIC %run "../../the_functions"

  1. Receive error:

Cell In[8], line 1 with databricks_notebook_exec_env('c:\Users\username\Documents\working_directory', '"../../the_functions"') as file: ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

TumbleTuesday avatar Mar 26 '25 14:03 TumbleTuesday

The last error you provided shows a stack trace with an old databricks_notebook_exec_env call. In the new version the arguments should be wrapped in r'...' strings and the quotes should be stripped from the file path.

Seems like the ipython startup script hasn't been updated. Can you make sure you've updated to the latest 2.9.0 version, then run "Databricks: Verify Databricks notebook init scripts" command, and see if there are any error notifications. Also make sure to restart your jupyter kernel (if you haven't done that already).

ilia-db avatar Mar 26 '25 14:03 ilia-db

Hi @ilia-db, thanks for the quick reply.

I have just run "Databricks: Verify Databricks notebook init scripts" and restarted the kernel and still seem to receive the same.

I restarted the kernel and even created a new clone of a repo and a new .venv environment in a completely different directory. The kernel is the .venv environment.

Any suggestions?

Image

TumbleTuesday avatar Mar 26 '25 15:03 TumbleTuesday

The startup script lives outside of the repo or venv - it's system-global and located in the HOME/.ipython/profile_default/startup/00-databricks-init-***.py. Running the "Verify Databricks notebook init scripts" command should override it though, but looking at the code it has a weird logic - it won't do anything unless you have a notebook open as an active editor. But the extension should update the init script automatically when you open a file, so not sure why it doesn't do it.

Can we confirm that the script is outdated? Can you open it and past the contents of the file here?

It's also possible that the HOME/.ipython/profile_default/startup/ folder has old scripts that the extension failed to delete (e.g. due to permission problems). Do you see multiple 00-databricks-init-***.py files there?

ilia-db avatar Mar 26 '25 15:03 ilia-db

There were indeed 4 different 00-databricks-init-***.py files in HOME/.ipython/profile_default/startup/. I have deleted the older ones and the command now doesn't produce the error! So its possible the old init files were not being cleared down when they were supposed to? Nevertheless it clearly runs the file now.

However I do still get this when it attempts to run it the file:

c:\Users\username\Documents\working_directory\.venv\Lib\site-packages\IPython\core\interactiveshell.py:2946: UserWarning: Could not open file <C:\Users\username\AppData\Local\Temp\tmp5p0e3lr9.ipynb> for safe execution.
  warn('Could not open file <%s> for safe execution.' % fname)

Might be worth noting if i remove the % from the %run command it runs the notebook file

TumbleTuesday avatar Mar 26 '25 15:03 TumbleTuesday

I've found a problem with our cleanup logic on windows, it should be fixed in the next release.

About the second issue: when you use %run with a .py Databricks notebook, we transform it to a "proper" .ipynb notebook, save it to a temp folder, and then pass it to the native run jupyter command. You can indeed use the run yourself, but without the transformation your file will be executed as a plain python file, and so any possible magic commands won't work there (plus we also append project root to the sys.path, without which some imports might not work).

Trying to reproduce this on my windows vm, but no luck so far

ilia-db avatar Mar 27 '25 11:03 ilia-db

Maybe you have some disabled permissions on the Temp folder? (specifically for a user under which you launch vscode)

Right click on the folder in the files-manager, then Properties, then Security tab:

Image

ilia-db avatar Mar 27 '25 11:03 ilia-db

@ilia-db Looking at the permissions on Temp, all users shown have the same level of permission (everything allowed except special permissions)

TumbleTuesday avatar Mar 27 '25 12:03 TumbleTuesday

Finally reproduced the bug on my side, will be fixed in the next release

ilia-db avatar Mar 27 '25 15:03 ilia-db

Hi @ilia-db , thanks for all your work on this, you're the 🐐. The %run command now works and imports the notebook and runs it.

However, when debugging a file which has an import like this, e.g.

# COMMAND ----------

# MAGIC %run "../../the_functions"

the functions are not available and the debugging results in an error. This occurs even when using the Debug current file with Databricks Connect button.

I assume this is the expected behaviour but I was wondering if it is possible to make this work?

We are working on changing over %run commands into proper pythonic package imports but in the meantime this would really help.

TumbleTuesday avatar Mar 31 '25 14:03 TumbleTuesday