<?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: ModuleNotFoundError / SerializationError when executing over databricks-connect in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/modulenotfounderror-serializationerror-when-executing-over/m-p/14302#M8818</link>
    <description>&lt;P&gt;@Sarosh Ahmad​&amp;nbsp;, You haven't provided all the details, but the issue is so close to one I've seen in the past, I'm fairly the certain is the same issue.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Long story short: when the executor executes a UDF, it will, &lt;B&gt;regardless of the function you register&lt;/B&gt;, attempt to execute the function using a fully qualified namespace.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That is to say, if you create a file like optimize_assortments/&lt;A href="https://foo.py" alt="https://foo.py" target="_blank"&gt;foo.py&lt;/A&gt;:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;def hello():
   ...
&amp;nbsp;
hello_udf = udf(hello, StringType())
df = (spark.sql('SELECT * FROM foo').withColumn('hello', hello_udf()))&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then the spark executor will attempt to execute `optimize_assortments.foo.hello` which is not defined, and the dreaded ModuleNotFoundError will be thrown.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is because the function 'hello' is &lt;B&gt;scoped &lt;/B&gt;to the &lt;B&gt;module that it is defined in. &lt;/B&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can resolve this by defining a function level UDF which has no scope, and is thus resolved as 'hello' when it is invoked, like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;def run():
  def hello():
     ...
 
  hello_udf = udf(hello, StringType())
  df = (spark.sql('SELECT * FROM foo').withColumn('hello', hello_udf()))&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Many people will recommend this approach for many reasons, but largely they have no idea why.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The reason is because when you define a function &lt;I&gt;inside &lt;/I&gt;a function, it is not module scoped, and therefore has no module namespace.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This code will work fine in a notebook (rather than in databricks connect) because notebooks use a single top level (ie. no namespace) module scope.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've tried to explain, but you can read more and see a full detailed example on stack overflow here -&amp;gt; &lt;A href="https://stackoverflow.com/questions/59322622/how-to-use-a-udf-defined-in-a-sub-module-in-pyspark/67158697#67158697" alt="https://stackoverflow.com/questions/59322622/how-to-use-a-udf-defined-in-a-sub-module-in-pyspark/67158697#67158697" target="_blank"&gt;https://stackoverflow.com/questions/59322622/how-to-use-a-udf-defined-in-a-sub-module-in-pyspark/67158697#67158697&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 28 Sep 2021 03:27:12 GMT</pubDate>
    <dc:creator>DouglasLinder</dc:creator>
    <dc:date>2021-09-28T03:27:12Z</dc:date>
    <item>
      <title>ModuleNotFoundError / SerializationError when executing over databricks-connect</title>
      <link>https://community.databricks.com/t5/data-engineering/modulenotfounderror-serializationerror-when-executing-over/m-p/14301#M8817</link>
      <description>&lt;P&gt;I am running into the following error when I run  a model fitting process over databricks-connect.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="modulenotfoundanno"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2415iD298387CEDE2E176/image-size/large?v=v2&amp;amp;px=999" role="button" title="modulenotfoundanno" alt="modulenotfoundanno" /&gt;&lt;/span&gt;It looks like worker nodes are unable to access modules from the project's parent directory. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note that the program runs successfully up to this point; no module not found errors are raised in the beginning and spark actions run just fine until this collect statement is called. Also, I have packaged my project as a wheel and installed it directly on the cluster to ensure the module is available to workers.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have the python project set up as follows:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;optimize-assortments&lt;/P&gt;&lt;P&gt;| - configs/&lt;/P&gt;&lt;P&gt;| - tests/&lt;/P&gt;&lt;P&gt;| -optimize_assortments/&lt;/P&gt;&lt;P&gt;     | - &lt;A href="http://process.py" alt="http://process.py" target="_blank"&gt;process.py&lt;/A&gt;&lt;/P&gt;&lt;P&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;| - sub_process_1.py&lt;/P&gt;&lt;P&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;| - sub_process_2.py&lt;/P&gt;&lt;P&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;| - sub_process_3.py&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://process.py" alt="http://process.py" target="_blank"&gt;process.py&lt;/A&gt; imports classes from each sub_process in module_1, instantiates and runs their methods. They are a collection of spark transformations along with a Pandas UDF, which fits a sci-kit model distributed across worker nodes. The error is raised after some subprocesses execute spark commands successfully across workers.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Some things I've tried/verified:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Python/DBR/ db-connect versions.&lt;/LI&gt;&lt;LI&gt;Moving all code from the sub_module into the parent Process.&lt;/LI&gt;&lt;LI&gt;Building a wheel and installing it on my cluster:&lt;UL&gt;&lt;LI&gt;Running via databricks-connect gives me ModuleNotFoundError halfway through execution as described above.&lt;/LI&gt;&lt;LI&gt;&lt;B&gt;If I import the module/submodule from in a Databricks notebook, the code executes successfully. &lt;/B&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Mon, 27 Sep 2021 20:36:38 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/modulenotfounderror-serializationerror-when-executing-over/m-p/14301#M8817</guid>
      <dc:creator>sarosh</dc:creator>
      <dc:date>2021-09-27T20:36:38Z</dc:date>
    </item>
    <item>
      <title>Re: ModuleNotFoundError / SerializationError when executing over databricks-connect</title>
      <link>https://community.databricks.com/t5/data-engineering/modulenotfounderror-serializationerror-when-executing-over/m-p/14302#M8818</link>
      <description>&lt;P&gt;@Sarosh Ahmad​&amp;nbsp;, You haven't provided all the details, but the issue is so close to one I've seen in the past, I'm fairly the certain is the same issue.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Long story short: when the executor executes a UDF, it will, &lt;B&gt;regardless of the function you register&lt;/B&gt;, attempt to execute the function using a fully qualified namespace.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That is to say, if you create a file like optimize_assortments/&lt;A href="https://foo.py" alt="https://foo.py" target="_blank"&gt;foo.py&lt;/A&gt;:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;def hello():
   ...
&amp;nbsp;
hello_udf = udf(hello, StringType())
df = (spark.sql('SELECT * FROM foo').withColumn('hello', hello_udf()))&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then the spark executor will attempt to execute `optimize_assortments.foo.hello` which is not defined, and the dreaded ModuleNotFoundError will be thrown.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is because the function 'hello' is &lt;B&gt;scoped &lt;/B&gt;to the &lt;B&gt;module that it is defined in. &lt;/B&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can resolve this by defining a function level UDF which has no scope, and is thus resolved as 'hello' when it is invoked, like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;def run():
  def hello():
     ...
 
  hello_udf = udf(hello, StringType())
  df = (spark.sql('SELECT * FROM foo').withColumn('hello', hello_udf()))&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Many people will recommend this approach for many reasons, but largely they have no idea why.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The reason is because when you define a function &lt;I&gt;inside &lt;/I&gt;a function, it is not module scoped, and therefore has no module namespace.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This code will work fine in a notebook (rather than in databricks connect) because notebooks use a single top level (ie. no namespace) module scope.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've tried to explain, but you can read more and see a full detailed example on stack overflow here -&amp;gt; &lt;A href="https://stackoverflow.com/questions/59322622/how-to-use-a-udf-defined-in-a-sub-module-in-pyspark/67158697#67158697" alt="https://stackoverflow.com/questions/59322622/how-to-use-a-udf-defined-in-a-sub-module-in-pyspark/67158697#67158697" target="_blank"&gt;https://stackoverflow.com/questions/59322622/how-to-use-a-udf-defined-in-a-sub-module-in-pyspark/67158697#67158697&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 03:27:12 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/modulenotfounderror-serializationerror-when-executing-over/m-p/14302#M8818</guid>
      <dc:creator>DouglasLinder</dc:creator>
      <dc:date>2021-09-28T03:27:12Z</dc:date>
    </item>
    <item>
      <title>Re: ModuleNotFoundError / SerializationError when executing over databricks-connect</title>
      <link>https://community.databricks.com/t5/data-engineering/modulenotfounderror-serializationerror-when-executing-over/m-p/14303#M8819</link>
      <description>&lt;P&gt;@Sarosh Ahmad​&amp;nbsp;, Could you try adding the zip of the module to the addPyFile&amp;nbsp;like below&lt;/P&gt;&lt;P&gt;spark.sparkContext.addPyFile("src.zip")&lt;/P&gt;</description>
      <pubDate>Tue, 02 Nov 2021 07:01:43 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/modulenotfounderror-serializationerror-when-executing-over/m-p/14303#M8819</guid>
      <dc:creator>Manjunath</dc:creator>
      <dc:date>2021-11-02T07:01:43Z</dc:date>
    </item>
  </channel>
</rss>

