Michael
Michael

Reputation: 407

Python setuptools: __init__.py does not call declare_namespace()

I am trying to install the zope2.zodbbrowser 0.2 package (https://pypi.python.org/pypi/zope2.zodbbrowser/0.2). The setup script fails with the following error:

Namespace package problem: zope2 is a namespace package, but its
__init__.py does not call declare_namespace()! Please fix it.
(See the setuptools manual under "Namespace Packages" for details.)

The relevant section in the manual seems to be this one here: https://pythonhosted.org/setuptools/setuptools.html#namespace-packages

However, I am not familiar with the internals of setuptools and I could not resolve the problem with the help of the manual. How can I resolve this namespace problem to successfully install the package?

For your reference, the source code of ~/zodbbrowser/src/zope2.zodbbrowser/zope2/__ init__.py is:

# this is a namespace package
try:
    import pkg_resources
    pkg_resources.declare_namespace(__name__)
except ImportError:
    import pkgutil
    __path__ = pkgutil.extend_path(__path__, __name__)

Upvotes: 1

Views: 3054

Answers (1)

Pierre.Sassoulas
Pierre.Sassoulas

Reputation: 4282

The error message tells you to use the declare_namespace() function, so it seems to be possible to make it work that way.

But you should probably use an implicit namespace package and remove the __init__.py entirely. Namespace packages generally do not have one because they can conflict with __init__.py from another namespace package with the same arborescence.

For example, if you have the zope2.a namespace:

zope2/__ init__.py
zope2/a/__init__.py

And the zope2.b namespace:

zope2/__ init__.py
zope2/b/__init__.py

The result after install the two would be:

zope2/__ init__.py <= Setuptools cannot know which file to take
zope2/a/__init__.py
zope2/b/__init__.py

See the Python documentation:

All that is required to create a native namespace package is that you just omit init.py from the namespace package directory.

https://packaging.python.org/guides/packaging-namespace-packages/

Upvotes: 1

Related Questions