Advanced concepts

Namespace packages

Hierarchical package structure

Traditionally, Python packages were organized into a hierarchical structure with modules and subpackages being located inside the parent package directory. When submodules are imported, they are represented as attributes on the parent module. Consider the following session:

>>> import sphinx.addnodes
>>> sphinx
<module 'sphinx' from '/usr/lib/python3.8/site-packages/sphinx/'>
>>> sphinx.addnodes
<module 'sphinx.addnodes' from '/usr/lib/python3.8/site-packages/sphinx/'>

This works fine most of the time. However, it start being problematic when multiple Gentoo packages install parts of the same top-level package. This may happen e.g. with some plugin layouts where plugins are installed inside the package. More commonly, it happens when upstream wishes all their packages to start with a common component.

This is the case with Zope framework. Different Zope packages share common zope top-level package. dev-python/zope-interface installs into zope.interface, dev-python/zope-event into zope.event. For this to work using the hierarchical layout, a common package has to install zope/, then other Zope packages have to depend on it and install sub-packages inside that directory. As far as installed packages are concerned, this is entirely doable.

The real problem happens when we wish to test a freshly built package that depends on an installed package. In that case, Python imports zope from build directory that contains only zope.interface. It will not be able to import zope.event that is installed in system package directory:

>>> import zope.interface
>>> zope
<module 'zope' from '/tmp/portage/dev-python/zope-interface-4.7.1/work/zope.interface-4.7.1-python3_8/lib/zope/'>
>>> zope.interface
<module 'zope.interface' from '/tmp/portage/dev-python/zope-interface-4.7.1/work/zope.interface-4.7.1-python3_8/lib/zope/interface/'>
>>> import zope.event
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'zope.event'

Now, this could be worked around by copying all other subpackages back to the build directory. However, there is a better solution.

Namespace package structure

Unlike traditional packages, namespace packages act as a kind of proxy. They are not strictly bound to the containing directory, and instead permit loading subpackages from all directories found in module search path. If we make zope a namespace package, we can import both the locally built zope.interface and system zope.event packages:

>>> import zope.interface
>>> import zope.event
>>> zope
<module 'zope' (namespace)>
>>> zope.interface
<module 'zope.interface' from '/tmp/portage/dev-python/zope-interface-4.7.1/work/zope.interface-4.7.1-python3_8/lib/zope/interface/'>
>>> zope.event
<module 'zope.event' from '/usr/lib/python3.8/site-packages/zope/event/'>

There are three common methods of creating namespace packages:

  1. PEP 420 namespaces implemented in Python 3.3 and newer,

  2. Using pkgutil standard library module,

  3. Using namespace package support in setuptools (discouraged).

PEP 420 namespaces are created implicitly when a package directory does not contain file. While earlier versions of Python (including Python 2.7) ignored such directories and did not permit importing Python modules within them, Python 3.3 imports such directories as namespace packages.

pkgutil namespaces use with the following content:

__path__ = __import__('pkgutil').extend_path(__path__, __name__)

setuptools namespace can use with the following content:


Alternatively, setuptools normally installs a .pth file that is automatically loaded by Python and implicitly injects the namespace into Python.

Both pkgutil and setuptools namespaces are portable to all versions of Python.

More general information on the topic can be found under packaging namespace packages in Python Packaging User Guide.

Determining whether namespaces are used

The exact method of detecting namespace packages depends on the type of namespace used.

PEP 420 namespaces can generally be recognized by the lack of in an installed package directory. However, since they do not require any specific action, distinguishing them is not very important.

pkgutil namespaces can be recognized through the content of their Generally, you should find it suspicious if it is the only file in a top-level package directory, and if the name of this directory is less specific than the package name (e.g. zope for zope.interface, ruamel for ruamel.yaml). If you miss this, then you will learn about it when the file collides between multiple packages.

setuptools namespaces usually do not install but do install a .pth file instead. Prior to installation, they can also be recognized by namespace_packages option in or setup.cfg. However, some packages use a custom file that does enable setuptools namespaces.

Adding new namespace packages to Gentoo

If the package uses PEP 420 namespaces, no special action is required. Per PEP 420 layout, the package must not install files for namespaces.

If the package uses the regular setuptools namespace install method (i.e. namespace_packages option), then the eclass detects that and strips the namespaces automatically, e.g.:

* python3_11: running distutils-r1_run_phase distutils-r1_python_install
* Stripping pkg_resources-style namespace ruamel
* Stripping pkg_resources-style namespace ruamel.std

If the package uses pkgutil-style or setuptools-style namespaces via files, these files need to be removed manually. This is done after the PEP 517 build phase:

python_compile() {
    rm "${BUILD_DIR}/install$(python_get_sitedir)"/jaraco/ || die

Note that in some extreme cases, upstream combines namespace support and other code in the file. Naturally, this file cannot be removed. No good solution has been found for this problem yet.

Some packages include an explicit setuptools runtime dependency (install_requires) when using namespaces. If this is the only use of pkg_resources and setuptools in installed package sources, this dependency needs to be stripped, e.g.:

src_prepare() {
    # strip rdep specific to namespaces
    sed -i -e "/'setuptools'/d" || die

Legacy namespace packages in Gentoo

Historically, Gentoo has used dev-python/namespace-* packages to support namespaces. This method is deprecated and it is in process of being retired.