You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Clarify __init__.py syntax in namespace packaging guide #1305
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:
does "only" mean "a semantically equivalent set of instructions that appear first in the file"?
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
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 ;-)
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!
(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 wellvarx=1vary=True
sp2/np/__init__.py
print("from sp2")
varx=2varz=True
Here is the behavior:
% PYTHONPATH=sp1:sp2 pythonPython 3.11.5 (main, Aug 26 2023, 14:36:58) [GCC 13.2.1 20230801] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import npfrom sp1
Here we see that only sp1/np/__init__.py was executed.
It make sense as it appeared first in the search paths.
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.
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.
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).
Activity
chrysle commentedon Sep 21, 2023
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!
You could open an issue at the Python Packaging User Guide next time ;-)
pawamoy commentedon Sep 21, 2023
Thanks!
I'd gladly open a PR, but I'm still not sure about the exact requirements for the
__init__.pyfile contents. Should I open the exact same issue on https://github.com/pypa/packaging.python.org? I see that you'reso 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.
pawamoy commentedon Sep 21, 2023
OK, interesting.
Given the following file tree:
(
spstands for search path,npstands for namespace package)sp1/np/__init__.pysp2/np/__init__.pyHere is the behavior:
Here we see that only
sp1/np/__init__.pywas 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
npto be a namespace package,with
sp2/npa part of it. It's probably thanks to theextend_pathcallin
sp1/np/__init__.py. Interestingly, though the docs say we needto put the same contents in all
__init__.pymodules,it's actually not strictly necessary.
Obviously, to make it work for any given search path order,
all of them must call
extend_pathindeed.Here we confirm that we can find both modules,
coming from both namespace packages.
Here we confirm again that only the first
__init__.pymodule was executed.With this I believe I'll be able to send a PR 🙂
chrysle commentedon Sep 21, 2023
Everything is possible with modern technology™!
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).
Thanks a lot for extensive testing!
[-]Question about the docs wording[/-][+]Clarify `__init__.py` syntax in namespace packaging guide[/+]