<?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: Pytest imports of sibling modules when using Databricks for VSCode in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/pytest-imports-of-sibling-modules-when-using-databricks-for/m-p/53449#M29800</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;Import errors happen often with Pytest. To Debug this error you can add this in your "test_myfunction_test.py":&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import sys 
# printing all directories for  
# interpreter to search 
sys.path&lt;/LI-CODE&gt;&lt;P&gt;&lt;STRONG&gt;sys.path&lt;/STRONG&gt; is a built-in variable within the sys module. It contains a list of directories that the interpreter will search in for the required module&lt;/P&gt;&lt;P&gt;In order to do&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;f&lt;/EM&gt;&lt;EM&gt;rom src.myfunctionfile,&lt;/EM&gt;&amp;nbsp;&lt;/STRONG&gt;your project directory needs to be in the list of "sys.path" paths&amp;nbsp;&lt;/P&gt;&lt;P&gt;A quick fix would be to use&lt;/P&gt;&lt;LI-CODE lang="python"&gt;sys.path.append("/you/project/directory") &lt;/LI-CODE&gt;&lt;P&gt;Otherwise you should check how your &lt;STRONG&gt;PYTHONPATH&lt;/STRONG&gt; variable is set or your &lt;STRONG&gt;pytest.ini&lt;/STRONG&gt; file&lt;BR /&gt;&lt;BR /&gt;Hope this helps &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 22 Nov 2023 09:00:38 GMT</pubDate>
    <dc:creator>Trifa</dc:creator>
    <dc:date>2023-11-22T09:00:38Z</dc:date>
    <item>
      <title>Pytest imports of sibling modules when using Databricks for VSCode</title>
      <link>https://community.databricks.com/t5/data-engineering/pytest-imports-of-sibling-modules-when-using-databricks-for/m-p/50744#M28883</link>
      <description>&lt;P&gt;Hello all,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I am following the Databrick's documentation on unit testing found here:&amp;nbsp;&lt;A href="https://learn.microsoft.com/en-us/azure/databricks/dev-tools/vscode-ext/dev-tasks/pytest" target="_blank" rel="noopener"&gt;Run tests with pytest for the Databricks extension for Visual Studio Code - Azure Databricks | Microsoft Learn&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;However, when taking it a step further I get an &lt;STRONG&gt;ImportError&lt;/STRONG&gt; or a &lt;STRONG&gt;ModuleNotFoundError&lt;/STRONG&gt;, when attempting a relative and absolute import of a sibling package, respectively.&amp;nbsp;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;My current folder structure is:&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;.&lt;BR /&gt;myinvoke.py
    src
        __init__.py
        myfunctionfile.py 
    tests
        test_myfunction_test.py&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;test_myfunction_test.py unit tests myfunction with the follow import: &lt;STRONG&gt;&lt;EM&gt;f&lt;/EM&gt;&lt;EM&gt;rom src.myfunctionfile import myfunction.&amp;nbsp;&lt;/EM&gt;&lt;/STRONG&gt;I have tried both relative and absolute imports for this with no result.&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I run the test as per the tutorial in the link above,through the VSCode Databricks extension, it runs myinvoke.py by invoking&amp;nbsp;&lt;SPAN&gt;pytest.main(), as expected, but pytest gets interrupted when collecting the test_myfunction_test function tests with&lt;STRONG&gt; ImportError: attempted relative import beyond top-level package&lt;/STRONG&gt;.&lt;BR /&gt;&lt;BR /&gt;These errors don't occur when running pytest locally. Why does the cluster env not recognize these packages?&lt;BR /&gt;&lt;BR /&gt;Thank you!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 15:42:23 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/pytest-imports-of-sibling-modules-when-using-databricks-for/m-p/50744#M28883</guid>
      <dc:creator>TaBorjaTa</dc:creator>
      <dc:date>2023-11-09T15:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: Pytest imports of sibling modules when using Databricks for VSCode</title>
      <link>https://community.databricks.com/t5/data-engineering/pytest-imports-of-sibling-modules-when-using-databricks-for/m-p/53449#M29800</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;Import errors happen often with Pytest. To Debug this error you can add this in your "test_myfunction_test.py":&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import sys 
# printing all directories for  
# interpreter to search 
sys.path&lt;/LI-CODE&gt;&lt;P&gt;&lt;STRONG&gt;sys.path&lt;/STRONG&gt; is a built-in variable within the sys module. It contains a list of directories that the interpreter will search in for the required module&lt;/P&gt;&lt;P&gt;In order to do&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;f&lt;/EM&gt;&lt;EM&gt;rom src.myfunctionfile,&lt;/EM&gt;&amp;nbsp;&lt;/STRONG&gt;your project directory needs to be in the list of "sys.path" paths&amp;nbsp;&lt;/P&gt;&lt;P&gt;A quick fix would be to use&lt;/P&gt;&lt;LI-CODE lang="python"&gt;sys.path.append("/you/project/directory") &lt;/LI-CODE&gt;&lt;P&gt;Otherwise you should check how your &lt;STRONG&gt;PYTHONPATH&lt;/STRONG&gt; variable is set or your &lt;STRONG&gt;pytest.ini&lt;/STRONG&gt; file&lt;BR /&gt;&lt;BR /&gt;Hope this helps &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2023 09:00:38 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/pytest-imports-of-sibling-modules-when-using-databricks-for/m-p/53449#M29800</guid>
      <dc:creator>Trifa</dc:creator>
      <dc:date>2023-11-22T09:00:38Z</dc:date>
    </item>
  </channel>
</rss>

