Skip to content

packages are not recognized as packages if __init__.py as it should since python 3.3 #2766

Open
@12rambau

Description

@12rambau

I was workig with autoapi and I got weird errors related to relative imports:

astroid.exceptions.TooManyLevelsError: Relative import with too many levels (2) for module 'not_found_404'

I finally realized that all the packages that were not specifcally containing a __init__.py file were simply discarded due to this lines of code:

        if level:
            if self.package:
                level = level - 1
                package_name = self.name.rsplit(".", level)[0]
            elif (
                self.path
                and not os.path.exists(os.path.dirname(self.path[0]) + "/__init__.py")
                and os.path.exists(
                    os.path.dirname(self.path[0]) + "/" + modname.split(".")[0]
                )
            ):
                level = level - 1
                package_name = ""
            else:
                package_name = self.name.rsplit(".", level)[0]
            if level and self.name.count(".") < level:
                raise TooManyLevelsError(level=level, name=self.name)

It turns out that to be considered a package, a folder MUST have an __init__.py file. Can you expand a bit the "package" definition to any folder including python files ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions