Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
PackageLoader fails on namespace packages #1097
In the attached jinja_namespace_bug.zip, app.py prints "Hi"
A traceback occurs. This is due to python3.7 namespace packages having their
Instead of running the commands below, you can also extract the attached zip file and run app.py.
Traceback (most recent call last): File "./app.py", line 5, in <module> jinja_env = jinja2.Environment(loader=jinja2.PackageLoader('foo.bar', 'templates')) File "/home/io/code/language/python/3/snippets/.venv/lib/python3.7/site-packages/jinja2/loaders.py", line 224, in __init__ provider = get_provider(package_name) File "/home/io/code/language/python/3/snippets/.venv/lib/python3.7/site-packages/pkg_resources/__init__.py", line 364, in get_provider return _find_adapter(_provider_factories, loader)(module) File "/home/io/code/language/python/3/snippets/.venv/lib/python3.7/site-packages/pkg_resources/__init__.py", line 1392, in __init__ self.module_path = os.path.dirname(getattr(module, '__file__', '')) File "/usr/lib/python3.7/posixpath.py", line 156, in dirname p = os.fspath(p) TypeError: expected str, bytes or os.PathLike object, not NoneType
➤ ./app.py Traceback (most recent call last): File "./app.py", line 4, in <module> jinja_env = jinja2.Environment(loader=jinja2.PackageLoader('foo.bar', 'templates')) File "/home/io/code/language/python/3/snippets/.venv/lib/python3.7/site-packages/jinja2/loaders.py", line 254, in __init__ os.path.dirname(self._loader.get_filename(package_name)), package_path AttributeError: 'NoneType' object has no attribute 'get_filename'
This would seem to suggest that
Unlike in coveragepy, where
Ignore the current release, as pkg_resources doesn't support loading resources from namespace packages.
Namespace package contributors don't have to be directories, they can also be zip files, just like regular packages. Unlike normal zip packages though, there's no loader API for namespace zips, so we now have to manage opening, scanning, and loading files from the zip file ourselves.
I'm not enthusiastic about developing and maintaining all this, it seems out of scope for Jinja at this point. We could support a limited version of this by assuming a single location and skipping zip files, but ultimately Python needs to improve their loader or
OK, since your example isn't really using namespace packages for their intended use (multiple sources contributing to one package) and is instead just taking advantage of omitting