cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

How to run a notebook in a .py file in databricks

Littlesheep_
New Contributor

The situation is that my colleague was using pycharm and now needs to adapt to databricks. They are now doing their job by connecting VScode to databricks and run the .py file using databricks clusters.

The problem is they want to call a notebook in databricks in the .py file in VScode, the %run command is not working because this command only works in notebook. I am wondering if there is a way to do it?

Thanks so much!

3 REPLIES 3

Hkesharwani
Contributor II

Hi, 
One way I can think of to achieve this is by calling the rest API of Databricks job (Create a job with only one task)
Check out this documentation for more details.
https://docs.databricks.com/api/workspace/jobs/runnow

Harshit Kesharwani
Data engineer at Rsystema

jacovangelder
Honored Contributor

You can do so by adding your own dbutils function in your py file:

 

def get_dbutils():
    """
    This is to make your local env (and flake  happy."""
    from pyspark.sql import SparkSession

    spark = SparkSession.getActiveSession()
    if spark.conf.get("spark.databricks.service.client.enabled") == "true":
        from pyspark.dbutils import DBUtils

        return DBUtils(spark)
    else:
        import IPython

        return IPython.get_ipython().user_ns["dbutils"]

 


And then run the notebook from the py file using the following way:

 

get_dbutils().notebook.run('path/to/notebook', <timeout_in_seconds>) 

 

Good luck!

 

 

 

Rishabh_Tiwari
Community Manager
Community Manager

Hi @Littlesheep_ ,

Thank you for reaching out to our community! We're here to help you. 

To ensure we provide you with the best support, could you please take a moment to review the response and choose the one that best answers your question? Your feedback not only helps us assist you better but also benefits other community members who may have similar questions in the future.

If you found the answer helpful, consider giving it a kudo. If the response fully addresses your question, please mark it as the accepted solution. This will help us close the thread and ensure your question is resolved.

We appreciate your participation and are here to assist you further if you need it!

Thanks,

Rishabh

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group