I Tried sys.exit(0)(Python code) and dbutils.notebook.exit() on Databricks notebook. But both the option didn't work. Please suggest any other way to stop the execution of code after a specific cell in Databricks notebook.
            Asked
            
        
        
            Active
            
        
            Viewed 2.8k times
        
    16
            
            
        - 
                    raise exception? – Alex Ott Feb 19 '21 at 10:18
- 
                    @AlexOtt I Have tried this , but that is also not working. try: if(df.shape[0]==0 and df_1.shape[0]==0 and df_2.shape[0]==0): print('Stop the excution here') dbutils.notebook.exit('stop') else: pass except: print('ex') – sizo_abe Feb 19 '21 at 10:39
- 
                    1`dbuilts.notebook.exit()` is used when the notebook is called from another notebook, not when it's executed interactively. Just use `raise Exception("exit")` instead of it... – Alex Ott Feb 19 '21 at 10:52
- 
                    @AlexOtt Can you provide the code instead. That will be helpful. – sizo_abe Feb 19 '21 at 11:05
- 
                    I wrote it - it's just having the `raise` – Alex Ott Feb 19 '21 at 14:00
- 
                    @AlexOtt i tried this : if(df.shape[0]==0): raise Exception("exception"). But That didn't work. I believe that is what you were saying. – sizo_abe Feb 19 '21 at 14:40
2 Answers
32
            
            
        dbutils.notebook.exit() does not work because you need to put the string argument, it fails silently without it. To make it work you should write something like:
dbutils.notebook.exit("whatever reason to make it stop")
 
    
    
        Robert
        
- 321
- 3
- 3
- 
                    2while it is true that another answer shows the use of `.exit()`, what this answer adds is that if you do not pass a string argument, the call will fail silently. Perhaps this answer could have been a comment on the other answer. – hlongmore Sep 28 '21 at 19:35
7
            
            
        Just use this. And click on run all cell button on Databricks notebook. If the condition is satisfied then execution will be stopped. Worked for me.
if (df.shape[0]==0): dbutils.notebook.exit('stop')
 
    
    
        sizo_abe
        
- 411
- 1
- 3
- 13