<?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: Cannot import relative python paths in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/cannot-import-relative-python-paths/m-p/115171#M45038</link>
    <description>&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;This works as long as the script calling the module is indeed __main__; i've changed it a bit to make it more generic:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;LI-CODE lang="markup"&gt;import os
import sys

def find_module(path):
    while path:
        if os.path.basename(path) == "src":
            return path
        new_path = os.path.abspath(os.path.join(path, '..'))
        if new_path == path:  # Prevent infinite loop
            break
        path = new_path
    return None

# Start from the current directory
current_directory = os.getcwd()

# Find the source directory
source_directory = find_module(current_directory)

if source_directory:
    sys.path.append(source_directory)
    from src.core import MyModule
else:
    raise ImportError("Databricks messed up my filepaths. Please complain somewhere.")&lt;/LI-CODE&gt;</description>
    <pubDate>Thu, 10 Apr 2025 14:36:15 GMT</pubDate>
    <dc:creator>klaas</dc:creator>
    <dc:date>2025-04-10T14:36:15Z</dc:date>
    <item>
      <title>Cannot import relative python paths</title>
      <link>https://community.databricks.com/t5/data-engineering/cannot-import-relative-python-paths/m-p/68275#M33621</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;Some variations of this question have been asked before but there doesn't seem to be an answer for the following simple use case:&lt;/P&gt;&lt;P&gt;I have the following file structure on a Databricks Asset Bundles project:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;src
--dir1
----file1.py
--dir2
----file2.py&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There doesn't seem to be a way to import anything from file1.py to file2.py. I have tried (inside file2.py):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from src.dir1.file1 import something
from dir1.file1 import something
from ..dir1.file1 import something&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;None of these seem to work. I have tried appending os.abspath("..") and "../.." to sys.path, as well as passing ${workspace.file_path} to the task and appending that, to no avail.&lt;/P&gt;&lt;P&gt;Note that relative imports on the same level work so the following both work:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# src
# --file1.py
# --file2.py
# We are on file1.py
from file2 import something

# src
# --file1.py
# --dir2
# ----file2.py
# We are on file1.py
from dir2.file2 import something&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is something weird going on with the import paths when deploying DABs? I'd rather avoid having to deploy a full-on wheel if I can avoid it, as it adds a lot of unnecessary boilerplate.&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 06 May 2024 14:18:19 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cannot-import-relative-python-paths/m-p/68275#M33621</guid>
      <dc:creator>georgef</dc:creator>
      <dc:date>2024-05-06T14:18:19Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot import relative python paths</title>
      <link>https://community.databricks.com/t5/data-engineering/cannot-import-relative-python-paths/m-p/81655#M36383</link>
      <description>&lt;P&gt;Unfortunately none of the methods proposed here worked, so we ended up having to wrap our code in packages and installing those in the cluster before running. Not an ideal workflow, but works for projects that are easily decoupled from their orchestration.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2024 13:57:35 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cannot-import-relative-python-paths/m-p/81655#M36383</guid>
      <dc:creator>georgef</dc:creator>
      <dc:date>2024-08-02T13:57:35Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot import relative python paths</title>
      <link>https://community.databricks.com/t5/data-engineering/cannot-import-relative-python-paths/m-p/81658#M36384</link>
      <description>&lt;P&gt;Hi.&amp;nbsp; This was a long-standing issue for me too.&amp;nbsp; This solution may not be what is desired, but it works perfectly for my needs.&lt;/P&gt;&lt;P&gt;In my python code, I have this structure:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;if __name__ == '__main__':

    # directory structure where "mycode" is this code here, trying to import "mymodule"
    #  &amp;gt; main
    #    -- mymodule
    #    &amp;gt; subdir
    #      -- mycode

    # Get the current directory
    current_directory = os.getcwd()

    # Get the parent directory
    parent_directory = os.path.abspath(os.path.join(current_directory, '..'))

    # Add the parent directory to sys.path
    sys.path.append(parent_directory)

    from mymodule import myfunction   &lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 02 Aug 2024 14:11:00 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cannot-import-relative-python-paths/m-p/81658#M36384</guid>
      <dc:creator>m997al</dc:creator>
      <dc:date>2024-08-02T14:11:00Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot import relative python paths</title>
      <link>https://community.databricks.com/t5/data-engineering/cannot-import-relative-python-paths/m-p/115171#M45038</link>
      <description>&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;This works as long as the script calling the module is indeed __main__; i've changed it a bit to make it more generic:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;LI-CODE lang="markup"&gt;import os
import sys

def find_module(path):
    while path:
        if os.path.basename(path) == "src":
            return path
        new_path = os.path.abspath(os.path.join(path, '..'))
        if new_path == path:  # Prevent infinite loop
            break
        path = new_path
    return None

# Start from the current directory
current_directory = os.getcwd()

# Find the source directory
source_directory = find_module(current_directory)

if source_directory:
    sys.path.append(source_directory)
    from src.core import MyModule
else:
    raise ImportError("Databricks messed up my filepaths. Please complain somewhere.")&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 10 Apr 2025 14:36:15 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/cannot-import-relative-python-paths/m-p/115171#M45038</guid>
      <dc:creator>klaas</dc:creator>
      <dc:date>2025-04-10T14:36:15Z</dc:date>
    </item>
  </channel>
</rss>

