# How to implement namespace packages (as in PEP 420) via import hooks?

**URL:** <https://discuss.python.org/t/how-to-implement-namespace-packages-as-in-pep-420-via-import-hooks/15192>\
**Category:** Python Help\
**Created:** [April 20, 2022, 3:49pm UTC](https://discuss.python.org/t/how-to-implement-namespace-packages-as-in-pep-420-via-import-hooks/15192 "2022-04-20T15:49:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abravalheri](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/abravalheri/32/4990_2.png) [@abravalheri](https://discuss.python.org/u/abravalheri)\
**Post date:** [April 20, 2022, 3:49pm UTC](https://discuss.python.org/t/how-to-implement-namespace-packages-as-in-pep-420-via-import-hooks/15192/1 "2022-04-20T15:49:12Z")

</div>

In a [previous post in the context of PEP 660](https://discuss.python.org/t/pep-660-and-setuptools/14855/15) (editable installs), @pf_moore and I were discussing that currently it is difficult to handle namespace packages using a `MetaPathFinder`, the main challenge being dynamic path computation.  
After testing some PoCs during the weekend, I think I manage to find 2 possible implementations that would work for this scenario:

- [(A)](https://gist.github.com/abravalheri/7a9f184b6034710684be1da803675140#file-2_recreate_the_effects_of_namespacepath-md) Consider `_NamespacePath` to be an implementation detail and re-implement another “path class” that emulates the behaviour described in [5. The import system — Python 3.12.1 documentation](https://docs.python.org/3/reference/import.html#namespace-packages) :

- [(B)](https://gist.github.com/abravalheri/7a9f184b6034710684be1da803675140#file-3_use_a_path_entry_finder-md) Instead of using a `MetaPathFinder`, create a custom `PathEntryFinder` and add a “bogus” entry to `sys.path` just to trigger it. This way the existing `importlib.machinery.PathFinder` will take care of creating the `_NamespacePath` under the hood.  
Comments:

The code for both PoCs is available at [How to implement Namespace packages via `MetaPathFinder`? · GitHub](https://gist.github.com/abravalheri/7a9f184b6034710684be1da803675140).

I would like to use this discussion thread to collect some feedback and ask the following questions:

1. The assumption in (A) is that the expression “a custom iterable type” to refer to any iterable type that fulfils the requirements, not a specific class of the stdlib. Is this a fair assumption or is there any risks that internally Python checks for `_NamespacePath`?
2. Is `pkgutil` (and particularly `pkgutil.extend_path`) still considered first class citizen of the standard library or is it better to avoid it when writing new code?
3. Is the behaviour observed in (B), by any chance, accidental? For a PEP 660 implementation it would be nice if this behaviour could be considered stable.
4. Is there any conceptual problems with approaches (A) and (B)?
5. Is there any recommended/other way of implementing this feature? (The main use case I am interested is PEP 660).

---

<div class="post-metadata">

**Author:** ![abravalheri](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/abravalheri/32/4990_2.png) [@abravalheri](https://discuss.python.org/u/abravalheri)\
**Post date:** [April 29, 2022, 3:21pm UTC](https://discuss.python.org/t/how-to-implement-namespace-packages-as-in-pep-420-via-import-hooks/15192/2 "2022-04-29T15:21:23Z")

</div>

I opened a new issue in `pypa/packaging-problems`: [Uncertainty on how to implement namespace packages (as in PEP 420) via import hooks · Issue #588 · pypa/packaging-problems · GitHub](https://github.com/pypa/packaging-problems/issues/588).

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [April 29, 2022, 4:27pm UTC](https://discuss.python.org/t/how-to-implement-namespace-packages-as-in-pep-420-via-import-hooks/15192/3 "2022-04-29T16:27:05Z")

</div>

I think you probably need to open an issue against CPython asking for clarification on how to support namespace packages using importlib hooks. I don’t think the original PEP for implicit namespace packages, nor the Python documentation, addresses this question, and while it’s possible that the CPython core devs might say “sorry, this is a question of how do I use Python, we don’t provide help in writing your own code”, I think it’s worth asking.

To be clear, I don’t personally (either as the author of `editables` or as a core python developer) have any idea how to implement this, short of somehow reimplementing the whole of the namespace package support (and even that might not support having a namespace path supplied partly from a hook and partly from a filesystem directory).

If it _isn’t_ possible to support implicit namespace packages using an import hook, I consider this a flaw in the way implicit namespace packages are defined/implemented, and therefore something that needs to be addressed in core python. However, I think it’s possible that it might be considered as something that won’t get fixed. I’ve never used namespace packages, explicit or implicit, so I have no feel for the importance of this “dynamic path search” feature - clearly there was some need for it when the original PEP was written (as otherwise why add the complexity?) but I don’t know any details.

---

<div class="post-metadata">

**Author:** ![abravalheri](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/abravalheri/32/4990_2.png) [@abravalheri](https://discuss.python.org/u/abravalheri)\
**Post date:** [April 29, 2022, 9:43pm UTC](https://discuss.python.org/t/how-to-implement-namespace-packages-as-in-pep-420-via-import-hooks/15192/4 "2022-04-29T21:43:17Z")

</div>

Thank you very much Paul. I opened [Clarification about how to implement namespace packages (as in PEP 420) via import hooks · Issue #92054 · python/cpython · GitHub](https://github.com/python/cpython/issues/92054).
