<?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: How to instantiate Databricks spark context in a python script? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63611#M32297</link>
    <description>&lt;P&gt;Thanks for your responses.&lt;BR /&gt;I did try creating a spark context and feed it to create a spark session constructor, but I get an error from databricks which states I should not initialize a new context despite using SparkContext.getOrCreate() Method.&lt;BR /&gt;Tried the following as well:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;SparkSession.getActiveSession() (returns null)&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;&lt;LI&gt;getOrCreate method on SparkContext and SparkSession ( Asks for Master URL and app name, after which it states the databricks error: Should not intialize session or context in databricks if one already exists.)&lt;/LI&gt;&lt;/OL&gt;</description>
    <pubDate>Thu, 14 Mar 2024 01:52:51 GMT</pubDate>
    <dc:creator>Spartan101</dc:creator>
    <dc:date>2024-03-14T01:52:51Z</dc:date>
    <item>
      <title>How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/8881#M4409</link>
      <description>&lt;P&gt;I want to run a block of code in a script and not in a notebook on databricks, however I cannot properly instantiate the spark context without some error.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have tried ` SparkContext.getOrCreate()`, but this does not work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there a simple way to do this I am missing?&lt;/P&gt;</description>
      <pubDate>Wed, 22 Feb 2023 22:14:25 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/8881#M4409</guid>
      <dc:creator>ae20cg</dc:creator>
      <dc:date>2023-02-22T22:14:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/8882#M4410</link>
      <description>&lt;P&gt;Hi @Andrej Erkelens​&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can you please send error which you are getting while using above code?&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2023 04:29:47 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/8882#M4410</guid>
      <dc:creator>Ajay-Pandey</dc:creator>
      <dc:date>2023-02-23T04:29:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/8884#M4412</link>
      <description>&lt;P&gt;@Kaniz Fatma​&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hi, I have tried this but receive an error&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;`RuntimeError: A master URL must be set in your configuration` &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there something I am missing to use databricks cluster (AWS backend) in a py script? &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2023 17:11:03 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/8884#M4412</guid>
      <dc:creator>ae20cg</dc:creator>
      <dc:date>2023-02-23T17:11:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/8885#M4413</link>
      <description>&lt;P&gt;I have the same problem, and would be interested in a solution&lt;/P&gt;</description>
      <pubDate>Fri, 14 Apr 2023 21:21:10 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/8885#M4413</guid>
      <dc:creator>Andrei_Radulesc</dc:creator>
      <dc:date>2023-04-14T21:21:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/56542#M30593</link>
      <description>&lt;P&gt;Did this ever get addressed? I would like to use a Databricks notebook to launch a python-based child process (os.popen) that itself ultimately needs to use pyspark. When I try this, I either get told to supply a Master URL to the Spark context, or if I apply local[*] as master, I get told in an exception message on Spark interaction that Notebooks should use the shared context available via sc. This code is executing in a standalone python library being run by the subprocess (based on python, but not just a python script) launched from Notebook.&lt;/P&gt;&lt;P&gt;Is it simply disallowed to access Spark outside of the shared context sc? If so, how can we access that shared context from a standalone python library as I describe?&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jan 2024 21:06:17 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/56542#M30593</guid>
      <dc:creator>testing3</dc:creator>
      <dc:date>2024-01-05T21:06:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/56581#M30601</link>
      <description>&lt;P&gt;Thanks for information&lt;/P&gt;&lt;P class="lia-align-right"&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jan 2024 10:41:11 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/56581#M30601</guid>
      <dc:creator>mikifare</dc:creator>
      <dc:date>2024-01-07T10:41:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63453#M32242</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;I tried doing this and get master URL and app name error. I tried setting those and get an error message that asks not to create a spark session in databricks and use sparkcontext.getOrCreate() method instead.&lt;BR /&gt;But that leads to the same error. I used the getActiveSession method to verify the python script does not have access to a spark session.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2024 05:02:17 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63453#M32242</guid>
      <dc:creator>Spartan101</dc:creator>
      <dc:date>2024-03-13T05:02:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63510#M32260</link>
      <description>&lt;P&gt;Try this pay attention to import&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;from pyspark.sql import SparkSession
appName = "abc"

# Create a SparkSession
spark = SparkSession.builder \
    .appName(appName) \
    .getOrCreate()

# Your PySpark code blah blah

# Stop the SparkSession when done
spark.stop()&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 13 Mar 2024 10:51:54 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63510#M32260</guid>
      <dc:creator>MichTalebzadeh</dc:creator>
      <dc:date>2024-03-13T10:51:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63565#M32277</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/101814"&gt;@MichTalebzadeh&lt;/a&gt;&amp;nbsp;, but I have tried this to no avail, I get [MASTER_URL_NOT_SET] error, and when I try to set it, I get an error stating I cannot create another spark session. the getActiveSession() returns null from within the script, but returns the session when called from the notebook.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2024 14:55:03 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63565#M32277</guid>
      <dc:creator>Spartan101</dc:creator>
      <dc:date>2024-03-13T14:55:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63566#M32278</link>
      <description>&lt;P&gt;Thanks. Please send the full detail of error you are getting&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2024 15:03:39 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63566#M32278</guid>
      <dc:creator>MichTalebzadeh</dc:creator>
      <dc:date>2024-03-13T15:03:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63567#M32279</link>
      <description>&lt;P&gt;I came across a similar issue.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Please detail how you are executing the python script. Are you calling it from the web terminal? or from a notebook?&lt;BR /&gt;&lt;BR /&gt;Note: If you are calling it from the web terminal - your spark session wont be passed. You could create a local variable and pass it in if youd like. I have never gotten to that point yet tho&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2024 15:31:18 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63567#M32279</guid>
      <dc:creator>Kaizen</dc:creator>
      <dc:date>2024-03-13T15:31:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63586#M32286</link>
      <description>&lt;P&gt;Sadly, I have the errors in my corporate environment and I can't show the exact error details from this account.But it is quite close to that of&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/97608"&gt;@testing3&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2024 17:03:20 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63586#M32286</guid>
      <dc:creator>Spartan101</dc:creator>
      <dc:date>2024-03-13T17:03:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63587#M32287</link>
      <description>&lt;P&gt;I am running the script from databricks notebook : !streamlit run MyScript.py&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2024 17:04:09 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63587#M32287</guid>
      <dc:creator>Spartan101</dc:creator>
      <dc:date>2024-03-13T17:04:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63607#M32294</link>
      <description>&lt;P&gt;&lt;SPAN&gt;If you hit an issue related to an already existing Spark context, you can only have one SparkContext instance in a single JVM. In such cases, you can try the following approach&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pyspark import SparkContext
from pyspark.sql import SparkSession

# Check if a Spark context already exists
try:
    sc = SparkContext.getOrCreate()
    spark = SparkSession(sc)
    print("Using existing Spark context.")
except Exception as e:
    print("No existing Spark context found. Creating a new one.")
    sc = SparkContext()
    spark = SparkSession(sc)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Mar 2024 20:35:59 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63607#M32294</guid>
      <dc:creator>MichTalebzadeh</dc:creator>
      <dc:date>2024-03-13T20:35:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63611#M32297</link>
      <description>&lt;P&gt;Thanks for your responses.&lt;BR /&gt;I did try creating a spark context and feed it to create a spark session constructor, but I get an error from databricks which states I should not initialize a new context despite using SparkContext.getOrCreate() Method.&lt;BR /&gt;Tried the following as well:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;SparkSession.getActiveSession() (returns null)&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;&lt;LI&gt;getOrCreate method on SparkContext and SparkSession ( Asks for Master URL and app name, after which it states the databricks error: Should not intialize session or context in databricks if one already exists.)&lt;/LI&gt;&lt;/OL&gt;</description>
      <pubDate>Thu, 14 Mar 2024 01:52:51 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63611#M32297</guid>
      <dc:creator>Spartan101</dc:creator>
      <dc:date>2024-03-14T01:52:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63706#M32324</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="4"&gt;&lt;SPAN&gt;I believe running a Streamlit app directly from a Databricks notebook using&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT size="4"&gt;&lt;SPAN&gt; !streamlit run &amp;lt;python_code&amp;gt;.py&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&lt;SPAN&gt; i&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT face="arial,helvetica,sans-serif" size="4"&gt;&lt;SPAN&gt;s not the way to do it,&amp;nbsp; because Databricks notebooks are not designed to host Streamlit&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="4"&gt;&lt;SPAN&gt;OK try below&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Create a Databricks Runtime with Streamlit Pre-installed.&amp;nbsp;Configure Cluster, ehen creating a new Databricks cluster, select a runtime that includes Streamlit pre-installed. This eliminates the installation step.&lt;/P&gt;&lt;P&gt;Run the script: Within the notebook cell, simply execute the script directly:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;!streamlit run MyScript.py&lt;/LI-CODE&gt;&lt;P&gt;HTH&lt;/P&gt;&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif" size="4"&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;apps.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2024 14:47:44 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63706#M32324</guid>
      <dc:creator>MichTalebzadeh</dc:creator>
      <dc:date>2024-03-14T14:47:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63722#M32331</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/101814"&gt;@MichTalebzadeh&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;Once again, thanks for your replies.&lt;BR /&gt;My databricks cluster does come preinstalled with streamlit, and I have been running the script the way you mentioned.&lt;BR /&gt;I am going to try using alternatives to spark for the time being, and try with spark session isolation disabled as well.&lt;BR /&gt;&lt;BR /&gt;I appreciate you taking out time to respond to this issue.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2024 17:57:19 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/63722#M32331</guid>
      <dc:creator>Spartan101</dc:creator>
      <dc:date>2024-03-14T17:57:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to instantiate Databricks spark context in a python script?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/92657#M38495</link>
      <description>&lt;P&gt;Is there some solution for this.We got struck where a cluster having unity catalog is not able to get spark context.This is not allowing to use distributed nature of spark in databricks.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2024 14:31:05 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-instantiate-databricks-spark-context-in-a-python-script/m-p/92657#M38495</guid>
      <dc:creator>ayush007</dc:creator>
      <dc:date>2024-10-03T14:31:05Z</dc:date>
    </item>
  </channel>
</rss>

