Skip to main content

find_top_level

Identifies the highest-level directory containing an init.py file to ensure relative imports function correctly by avoiding the 'Attempted relative import in non-package' error. It iteratively traverses parent directories until no init.py is found or the root directory is reached.

def find_top_level(
top_level: string
) - > string

Determines the highest-level directory containing an init.py file to ensure relative imports function correctly within a package structure. This avoids the "Attempted relative import in non-package" error by preventing the test runner from incorrectly treating a sub-package as the root directory.

Parameters

NameTypeDescription
top_levelstringThe initial directory path from which to begin searching upwards for the package root.

Returns

TypeDescription
stringThe absolute or relative path to the root directory of the package, which is the parent of the highest directory containing an init.py file.