<?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: Caveats when importing functions from REPO stored .py files in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/caveats-when-importing-functions-from-repo-stored-py-files/m-p/10840#M5905</link>
    <description>&lt;P&gt;Thanks Hubert!  &lt;/P&gt;&lt;P&gt;Passing the spark variable is a great idea.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The display(HTML()) solution is working well.  Thanks for confirming displayHTML() isn't designed to be used outside of notebooks.&lt;/P&gt;</description>
    <pubDate>Tue, 24 Jan 2023 19:06:07 GMT</pubDate>
    <dc:creator>Craig_</dc:creator>
    <dc:date>2023-01-24T19:06:07Z</dc:date>
    <item>
      <title>Caveats when importing functions from REPO stored .py files</title>
      <link>https://community.databricks.com/t5/data-engineering/caveats-when-importing-functions-from-repo-stored-py-files/m-p/10837#M5902</link>
      <description>&lt;P&gt;The ability to import .py files into notebooks looked like a clean and easy way to reuse code and to ensure all notebooks are using the same version of code.  However, two items remain unclear after scouring documentation and forums.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Are these the right / best solutions to the problems or should we revert back to %run and notebooks instead of .py files?   Thanks!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;B&gt;Code within the .py file does not have access to the spark session by default.&lt;/B&gt;&lt;/P&gt;&lt;P&gt;Outcome:  &lt;I&gt;NameError: name 'spark' is not defined&lt;/I&gt;&lt;/P&gt;&lt;P&gt;Solution:  add the following to the .py file:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Are there any implications to this?  &lt;/P&gt;&lt;P&gt;Does the notebook code and .py code share the same session or does this cause separate sessions?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;B&gt;display() and displayHTML() functions are not available to the .py code by default&lt;/B&gt;&lt;/P&gt;&lt;P&gt;Outcome: &lt;I&gt;NameError: name 'displayHTML' is not defined&lt;/I&gt; when displayHTML() is called from within the .py file&lt;/P&gt;&lt;P&gt;Solution: add the following to the .py file and use display(HTML()) instead of displayHTML():&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from IPython.core.display import display, HTML  #How to use:  display(HTML("your content"))&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is there a better way to get displayHTML() working inside the .py file?  &lt;/P&gt;&lt;P&gt;What about all of the other databricks specific functions?&lt;/P&gt;</description>
      <pubDate>Mon, 23 Jan 2023 21:09:55 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/caveats-when-importing-functions-from-repo-stored-py-files/m-p/10837#M5902</guid>
      <dc:creator>Craig_</dc:creator>
      <dc:date>2023-01-23T21:09:55Z</dc:date>
    </item>
    <item>
      <title>Re: Caveats when importing functions from REPO stored .py files</title>
      <link>https://community.databricks.com/t5/data-engineering/caveats-when-importing-functions-from-repo-stored-py-files/m-p/10839#M5904</link>
      <description>&lt;UL&gt;&lt;LI&gt;"does not have access to the spark session by default" yes, that is correct you need to pass the reference to the spark variable inside Class or Function, something like when you call from notebook function_from_file(spark=spark)&lt;/LI&gt;&lt;LI&gt;displayHTML() is designed to be work from notebook&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Tue, 24 Jan 2023 18:34:31 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/caveats-when-importing-functions-from-repo-stored-py-files/m-p/10839#M5904</guid>
      <dc:creator>Hubert-Dudek</dc:creator>
      <dc:date>2023-01-24T18:34:31Z</dc:date>
    </item>
    <item>
      <title>Re: Caveats when importing functions from REPO stored .py files</title>
      <link>https://community.databricks.com/t5/data-engineering/caveats-when-importing-functions-from-repo-stored-py-files/m-p/10840#M5905</link>
      <description>&lt;P&gt;Thanks Hubert!  &lt;/P&gt;&lt;P&gt;Passing the spark variable is a great idea.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The display(HTML()) solution is working well.  Thanks for confirming displayHTML() isn't designed to be used outside of notebooks.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2023 19:06:07 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/caveats-when-importing-functions-from-repo-stored-py-files/m-p/10840#M5905</guid>
      <dc:creator>Craig_</dc:creator>
      <dc:date>2023-01-24T19:06:07Z</dc:date>
    </item>
    <item>
      <title>Re: Caveats when importing functions from REPO stored .py files</title>
      <link>https://community.databricks.com/t5/data-engineering/caveats-when-importing-functions-from-repo-stored-py-files/m-p/10841#M5906</link>
      <description>&lt;P&gt;This didnt work for me.  &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import PythonShell as pi&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;results in "&lt;I&gt;ImportWarning: PythonShell.py is a launch script, which shall not be imported directly. You might want to import PythonShellImpl instead.&lt;/I&gt;"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and then&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;pi.PythonShell.display("test")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;results in "&lt;I&gt;AttributeError: 'str' object has no attribute 'display'&lt;/I&gt;"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I tried different variations of import&lt;I&gt; PythonShellImpl which didn't work either.&lt;/I&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2023 19:19:38 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/caveats-when-importing-functions-from-repo-stored-py-files/m-p/10841#M5906</guid>
      <dc:creator>Craig_</dc:creator>
      <dc:date>2023-01-24T19:19:38Z</dc:date>
    </item>
    <item>
      <title>Re: Caveats when importing functions from REPO stored .py files</title>
      <link>https://community.databricks.com/t5/data-engineering/caveats-when-importing-functions-from-repo-stored-py-files/m-p/10838#M5903</link>
      <description>&lt;P&gt;Hi, In Databricks the display functions is coming from PythonShell&lt;/P&gt;&lt;P&gt;import PythonShell as pi&lt;/P&gt;&lt;P&gt;pi.PythonShell.display(temps)&lt;/P&gt;&lt;P&gt;This works in Databricks but this is not whitelisted for outside use.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Jan 2023 06:53:51 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/caveats-when-importing-functions-from-repo-stored-py-files/m-p/10838#M5903</guid>
      <dc:creator>Debayan</dc:creator>
      <dc:date>2023-01-24T06:53:51Z</dc:date>
    </item>
  </channel>
</rss>

