pylint issue w/ native namespace packages
Bug
Python 3.3 introduced native namespace packages with PEP 420:
All that is required to create a native namespace package is that you just omit init.py from the namespace package directory.
Unfortunately pylint
seems to have some issues w/ native namespace packages and prints a lot of import-error
errors, even if everything is running. This is only because pylint
won't work with namespace packages and nothing else. This is also mentioned in several issues:
This means while native namespace packages work fine, pylint >= 2.5
will have a problem with it. Fortunately, there is a workaround for this.
Workaround
Instead of using native namespace packages and omitting the __init__.py
in the namespace package, add an __init__.py
again and create an old-style namespace package:
pkgutil-style namespace packages:
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
pkg_resources-style namespace packages:
__import__('pkg_resources').declare_namespace(__name__)
The pkgutil
is preferred over pkg_resources
, as stated in the docs:
While this approach is no longer recommended, it is widely present in most existing namespace packages. If you are creating a new distribution within an existing namespace package that uses this method then it’s recommended to continue using this as the different methods are not cross-compatible and it’s not advisable to try to migrate an existing package.