Skip to content

Add support for pkgutil-style namespace packages #9393

Closed as not planned
Closed as not planned
@alxroyer

Description

@alxroyer

🚀 Feature

Add support for pkgutil-style namespace packages.

Pitch

In october 2018, PR #5691 brought support for PEP420 namespace packages (i.e. native namespace packages), which closed issue #1645.

However, the native PEP420 method only works for Python 3.

This type of namespace package is defined in PEP 420 and is available in Python 3.3 and later. This is recommended if packages in your namespace only ever need to support Python 3 and installation via pip.

According to Python documentation, for Python 2/3 compatible code, the pkgutil-style remains the recommended way.

This is recommended for new packages that need to support Python 2 and 3 and installation via both pip and python setup.py install.

Let the following project structure, with PYTHONPATH and MYPYPATH pointing to the src/ and 'tests/src/' directories:

my-project
+-- src/
|   +-- mynamespace/
|       +-- __init__.py
+-- tests/
    +-- data/
    +-- src/
        +-- mynamespace/
            +-- test/
            |   +-- __init__.py
            +-- __init__.py

The mynamespace namespace is splitted in two directories, in order not to mix up the test code with the deliverable code in this project.
Within both src/mynamespace/__init__.py and tests/src/mynamespace/__init__.py, the following code implements the pkgutil-style namespace package:

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

Python 2 and Python 3 execute this code correctly.

But, unfortunately, mypy (as of 0.770) generates a false 'Duplicate module named 'mynamespace'' error (same error as the former #1645 issue).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions