<?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 import a function to another notebook? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74608#M34764</link>
    <description>&lt;P&gt;I haven't, have you done exactly the steps outlined? It looks like perhaps you're not allowed to read form the shared location? Not 100% sure.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 17 Jun 2024 12:46:15 GMT</pubDate>
    <dc:creator>jacovangelder</dc:creator>
    <dc:date>2024-06-17T12:46:15Z</dc:date>
    <item>
      <title>How to import a function to another notebook?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74365#M34738</link>
      <description>&lt;P&gt;Could you please provide guidance on the correct way to dynamically import a Python module from a user-specific path in Databricks Repos? Any advice on resolving the ModuleNotFoundError would be greatly appreciated.&lt;/P&gt;&lt;P&gt;udf_check_table_exists notebook:&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;EM&gt;from pyspark.sql.utils import AnalysisException&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;import os&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;import sys&lt;/EM&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;EM&gt;def table_exists(table_name):&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp; try:&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; spark.table(table_name)&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return True&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp; except AnalysisException:&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return False&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Error&amp;nbsp;&lt;SPAN class=""&gt;ModuleNotFoundError: &lt;/SPAN&gt;No module named 'udf_check_table_exists' with another notebook:&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;EM&gt;import os&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;import sys&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;sys.path.append(os.path.abspath('/Workspace/Repos/[my repo]/dw/common_helper'))&lt;/EM&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;EM&gt;from udf_check_table_exists import table_exists&lt;/EM&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;Thank you for your assistance.&lt;/P&gt;</description>
      <pubDate>Sun, 16 Jun 2024 06:38:35 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74365#M34738</guid>
      <dc:creator>tramtran</dc:creator>
      <dc:date>2024-06-16T06:38:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to import a function to another notebook?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74523#M34753</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;There are two ways to import functions from other notebook:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;STRONG&gt;%run ../notebook path&lt;/STRONG&gt; : This command will run the entire notebook and the function along with all the variable names will be imported. [This function should ideally be used to import the functions from another notebook, if another notebook only contains function definition]&lt;/LI&gt;&lt;LI&gt;The second method to import function is for repos: In&amp;nbsp; repos we can easily&amp;nbsp; import&amp;nbsp; static .py files.&lt;BR /&gt;From folder name import function.&lt;BR /&gt;Refer to this documentation for more detail answer:&amp;nbsp;&lt;A href="https://www.databricks.com/blog/2021/10/07/databricks-repos-is-now-generally-available.html" target="_blank"&gt;https://www.databricks.com/blog/2021/10/07/databricks-repos-is-now-generally-available.html&lt;/A&gt;&lt;/LI&gt;&lt;/OL&gt;</description>
      <pubDate>Mon, 17 Jun 2024 05:03:23 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74523#M34753</guid>
      <dc:creator>Hkesharwani</dc:creator>
      <dc:date>2024-06-17T05:03:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to import a function to another notebook?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74563#M34755</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/99364"&gt;@Hkesharwani&lt;/a&gt;&amp;nbsp;for your replying,&lt;/P&gt;&lt;P&gt;As DLT doesn't support the magic command %run, that's why I'm trying the import function way&lt;/P&gt;&lt;P&gt;my layout like this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tramtran_0-1718612237964.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/8739i907086F92CEB567E/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="tramtran_0-1718612237964.png" alt="tramtran_0-1718612237964.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;other notebook:&lt;/P&gt;&lt;DIV&gt;&lt;EM&gt;import os&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;import sys&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;sys.path.append(os.path.abspath('/Workspace/Repos/[my repo]/dw/common_helper'))&lt;/EM&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;EM&gt;from udf_check_table_exists import table_exists&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;But it always returns&amp;nbsp;ModuleNotFoundError&amp;nbsp; error&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2024 08:23:08 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74563#M34755</guid>
      <dc:creator>tramtran</dc:creator>
      <dc:date>2024-06-17T08:23:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to import a function to another notebook?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74577#M34760</link>
      <description>&lt;P&gt;Hey&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/71255"&gt;@tramtran&lt;/a&gt;,&lt;BR /&gt;&lt;BR /&gt;I didn't try with repos, but it works with a Workspace path the following way (we're doing it like this so I validated it for you). I guess since Repos are also inside /Workspace, it will work the same way.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Lets use the default&amp;nbsp;&lt;STRONG&gt;/Workspace/Shared&lt;/STRONG&gt; folder for this example.&lt;BR /&gt;&lt;BR /&gt;1. Add the .py file with your table_exists function to the&amp;nbsp;&lt;STRONG&gt;/Workspace/Shared&lt;/STRONG&gt;&amp;nbsp;folder. Lets call the file&amp;nbsp;&lt;STRONG&gt;function_file.py&amp;nbsp;&lt;/STRONG&gt;for this example.&amp;nbsp;&lt;BR /&gt;2. Create an &lt;STRONG&gt;__init__.py&lt;/STRONG&gt; file in this&lt;STRONG&gt;/Workspace/Shared &lt;/STRONG&gt;directory&amp;nbsp;as well. So Databricks knows this is a package index.&amp;nbsp;&lt;BR /&gt;3. In the notebook you want to import the function from the .py file, add this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import sys
sys.path.append("/Workspace/Shared")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;then import the function like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from function_file import table_exists&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this helps, good luck!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2024 11:29:57 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74577#M34760</guid>
      <dc:creator>jacovangelder</dc:creator>
      <dc:date>2024-06-17T11:29:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to import a function to another notebook?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74602#M34763</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/102253"&gt;@jacovangelder&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;I did the same as your suggestion, but another error appeared:&lt;/P&gt;&lt;P&gt;OSError: [Errno 95] Operation not supported: '/Workspace/Shared/function_file.py'.&lt;/P&gt;&lt;P&gt;Have you faced this issue before?&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2024 12:18:16 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74602#M34763</guid>
      <dc:creator>tramtran</dc:creator>
      <dc:date>2024-06-17T12:18:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to import a function to another notebook?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74608#M34764</link>
      <description>&lt;P&gt;I haven't, have you done exactly the steps outlined? It looks like perhaps you're not allowed to read form the shared location? Not 100% sure.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2024 12:46:15 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74608#M34764</guid>
      <dc:creator>jacovangelder</dc:creator>
      <dc:date>2024-06-17T12:46:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to import a function to another notebook?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74665#M34773</link>
      <description>&lt;P&gt;It works for me now &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; by refer this solution:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/MicrosoftDocs/azure-docs/issues/113823" target="_blank"&gt;[Errno 95] Operation not supported · Issue #113823 · MicrosoftDocs/azure-docs · GitHub&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;creating a "file" instead of a "notebook" and moving the code from the notebook into the file, I was able to use the "import" statement&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;my import code:&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;EM&gt;import sys&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;EM&gt;sys.path.append("/Workspace/Shared/")&lt;/EM&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;EM&gt;from test_import_func import table_exists&lt;/EM&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;EM&gt;print(table_exists('mytable',spark))&lt;/EM&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2024 16:03:59 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74665#M34773</guid>
      <dc:creator>tramtran</dc:creator>
      <dc:date>2024-06-17T16:03:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to import a function to another notebook?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74666#M34774</link>
      <description>&lt;P&gt;Thank you all again&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2024 16:07:12 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-import-a-function-to-another-notebook/m-p/74666#M34774</guid>
      <dc:creator>tramtran</dc:creator>
      <dc:date>2024-06-17T16:07:12Z</dc:date>
    </item>
  </channel>
</rss>

