Skip to content

Clarify __init__.py syntax in namespace packaging guide #1305

Closed

Description

@pawamoy

The docs say:

The init.py file for the namespace package needs to contain only the following:
Every distribution that uses the namespace package must include an identical init.py

I wonder:

  1. does "only" mean "a semantically equivalent set of instructions that appear first in the file"?
  2. does "identical" mean "semantically equivalent" or "character-by-character identical"?

To give an example, consider the two following __init__.py modules. Are they valid paired-modules for a non-native namespace package?

# arbitrary comments like:
# pylint: disable=missing-module-docstring
__path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore
# note the type ignore comment as well
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

Note how neither the textual contents nor the AST is the same, but how they actually do the same thing.

Does it still work if there are other statements after extending __path__?

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
print("done extending __path__")

Don't hesitate to redirect me to the code responsible for this 🙂

Activity

  1. chrysle commented on Sep 21, 2023

    @chrysle
    Contributor
    1. does "only" mean "a semantically equivalent set of instructions that appear first in the file"?

    Yes and no. It needs to appear somewhere in the file, and of course it's good style to keep the imports at the beginning.
    I agree the wording is a bit ambiguous. We would welcome a pull request over at the guide to update that!

    Don't hesitate to redirect me to the code responsible for this 🙂

    You could open an issue at the Python Packaging User Guide next time ;-)

  2. pawamoy commented on Sep 21, 2023

    @pawamoy
    SponsorContributorAuthor

    Thanks!

    I'd gladly open a PR, but I'm still not sure about the exact requirements for the __init__.py file contents. Should I open the exact same issue on https://github.com/pypa/packaging.python.org? I see that you're

    looking into an overhaul of the guide

    so that might not be necessary? Maybe easier for you to track if it's over there. Let me know!

    In the meantime I'll run some experiments.

  3. pawamoy commented on Sep 21, 2023

    @pawamoy
    SponsorContributorAuthor

    OK, interesting.

    Given the following file tree:

    .
    ├── sp1
    │   └── np
    │       ├── __init__.py
    │       └── moda.py
    └── sp2
        └── np
            ├── __init__.py
            └── modb.py
    

    (sp stands for search path, np stands for namespace package)

    sp1/np/__init__.py

    print("from sp1")
    # arbitrary comments like:
    # pylint: disable=missing-module-docstring
    __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore
    # note the type ignore comment as well
    varx = 1
    vary = True

    sp2/np/__init__.py

    print("from sp2")
    varx = 2
    varz = True

    Here is the behavior:

    % PYTHONPATH=sp1:sp2 python
    Python 3.11.5 (main, Aug 26 2023, 14:36:58) [GCC 13.2.1 20230801] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import np
    from sp1

    Here we see that only sp1/np/__init__.py was executed.
    It make sense as it appeared first in the search paths.


    >>> np.__path__
    ['/home/pawamoy/t/sp1/np', '/home/pawamoy/t/sp2/np']

    Still, Python correctly finds np to be a namespace package,
    with sp2/np a part of it. It's probably thanks to the extend_path call
    in sp1/np/__init__.py. Interestingly, though the docs say we need
    to put the same contents in all __init__.py modules,
    it's actually not strictly necessary.
    Obviously, to make it work for any given search path order,
    all of them must call extend_path indeed.


    >>> from np import moda, modb
    >>> moda.__file__
    '/home/pawamoy/t/sp1/np/moda.py'
    >>> modb.__file__
    '/home/pawamoy/t/sp2/np/modb.py'

    Here we confirm that we can find both modules,
    coming from both namespace packages.


    >>> np.varx
    1
    >>> np.vary
    True
    >>> np.varz
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: module 'np' has no attribute 'varz'. Did you mean: 'varx'?

    Here we confirm again that only the first __init__.py module was executed.


    With this I believe I'll be able to send a PR 🙂

  4. chrysle commented on Sep 21, 2023

    @chrysle
    Contributor

    Should I open the exact same issue on https://github.com/pypa/packaging.python.org?

    Everything is possible with modern technology™!

    I see that you're

    looking into an overhaul of the guide

    so that might not be necessary? Maybe easier for you to track if it's over there. Let me know!

    With all your testing experience, I think you could do it even better than me! I'm also mostly aiming to cover some additional information (mostly from the setuptools docs).

    In the meantime I'll run some experiments.

    Thanks a lot for extensive testing!

  5. added
    type: enhancement
    on Sep 21, 2023
  6. changed the title [-]Question about the docs wording[/-] [+]Clarify `__init__.py` syntax in namespace packaging guide[/+] on Sep 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    component: guidestype: enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions