<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Databricks connect and spark session best practice in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/databricks-connect-and-spark-session-best-practice/m-p/126376#M10406</link>
    <description>&lt;P&gt;I personally do something similar by checking an environment variable, the example was for a notebook but should work for a python file as well:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os

if not os.environ.get("DATABRICKS_RUNTIME_VERSION"):
    from databricks.connect import DatabricksSession
    print("This notebook is running outside of Databricks. Using Databricks Connect...")
    spark = DatabricksSession.builder.getOrCreate()
    print("Runtime:", spark.conf.get("spark.databricks.clusterUsageTags.sparkVersion"))
else:
    print(
        "This notebook is running inside Databricks. Using the default Spark session...\nRuntime:",
        spark.conf.get("spark.databricks.clusterUsageTags.sparkVersion"),
    )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 24 Jul 2025 13:52:37 GMT</pubDate>
    <dc:creator>FedeRaimondi</dc:creator>
    <dc:date>2025-07-24T13:52:37Z</dc:date>
    <item>
      <title>Databricks connect and spark session best practice</title>
      <link>https://community.databricks.com/t5/get-started-discussions/databricks-connect-and-spark-session-best-practice/m-p/44048#M968</link>
      <description>&lt;P&gt;Hi all!&lt;/P&gt;&lt;P&gt;I am using databricks-connect to develop and test pyspark code pure python (not notebook) files in my local IDE, running on a Databricks cluster. These files are part of a deployment setup with dbx so that they are run as tasks in a workflow.&lt;/P&gt;&lt;P&gt;Everything works fine, but then there is this piece, to know whether to create a databricks connect spark session or reuse the spark session running in Databricks as part of a job :&lt;/P&gt;&lt;LI-CODE lang="python"&gt;try:
    from databricks.connect import DatabricksSession
    spark = DatabricksSession.builder.getOrCreate()
except ImportError:
    from pyspark.sql import SparkSession
    spark = SparkSession.builder.getOrCreate()&lt;/LI-CODE&gt;&lt;P&gt;And that feels like code smell. Is there a nicer way you would recommend to handle the spark session, whether running locally via databricks-connect or directly on Databricks?&lt;/P&gt;</description>
      <pubDate>Fri, 08 Sep 2023 05:46:57 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/databricks-connect-and-spark-session-best-practice/m-p/44048#M968</guid>
      <dc:creator>thibault</dc:creator>
      <dc:date>2023-09-08T05:46:57Z</dc:date>
    </item>
    <item>
      <title>Re: Databricks connect and spark session best practice</title>
      <link>https://community.databricks.com/t5/get-started-discussions/databricks-connect-and-spark-session-best-practice/m-p/44579#M1047</link>
      <description>&lt;P&gt;Thanks for your response&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/9"&gt;@Retired_mod&lt;/a&gt;&amp;nbsp;. Can you elaborate on the difference between your suggestion and the code I provided? i.e. what would your if-else look like?&lt;/P&gt;</description>
      <pubDate>Wed, 13 Sep 2023 07:07:50 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/databricks-connect-and-spark-session-best-practice/m-p/44579#M1047</guid>
      <dc:creator>thibault</dc:creator>
      <dc:date>2023-09-13T07:07:50Z</dc:date>
    </item>
    <item>
      <title>Re: Databricks connect and spark session best practice</title>
      <link>https://community.databricks.com/t5/get-started-discussions/databricks-connect-and-spark-session-best-practice/m-p/126142#M10401</link>
      <description>&lt;P&gt;By using the following command:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;sc.version&lt;/LI-CODE&gt;&lt;P&gt;you can refer to Spark version and apply Control Flow - as since version 3.4 its available, you can apply custom logic to consider the version of Spark and Control whether to use Spark Connect or Spark Session.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Jul 2025 10:15:46 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/databricks-connect-and-spark-session-best-practice/m-p/126142#M10401</guid>
      <dc:creator>bartoszmalec</dc:creator>
      <dc:date>2025-07-23T10:15:46Z</dc:date>
    </item>
    <item>
      <title>Re: Databricks connect and spark session best practice</title>
      <link>https://community.databricks.com/t5/get-started-discussions/databricks-connect-and-spark-session-best-practice/m-p/126376#M10406</link>
      <description>&lt;P&gt;I personally do something similar by checking an environment variable, the example was for a notebook but should work for a python file as well:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os

if not os.environ.get("DATABRICKS_RUNTIME_VERSION"):
    from databricks.connect import DatabricksSession
    print("This notebook is running outside of Databricks. Using Databricks Connect...")
    spark = DatabricksSession.builder.getOrCreate()
    print("Runtime:", spark.conf.get("spark.databricks.clusterUsageTags.sparkVersion"))
else:
    print(
        "This notebook is running inside Databricks. Using the default Spark session...\nRuntime:",
        spark.conf.get("spark.databricks.clusterUsageTags.sparkVersion"),
    )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Jul 2025 13:52:37 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/databricks-connect-and-spark-session-best-practice/m-p/126376#M10406</guid>
      <dc:creator>FedeRaimondi</dc:creator>
      <dc:date>2025-07-24T13:52:37Z</dc:date>
    </item>
  </channel>
</rss>

