Skip to content

PackageLoader fails on namespace packages #1097

@ioistired

Description

@ioistired

Expected Behavior

In the attached jinja_namespace_bug.zip, app.py prints "Hi"

Actual Behavior

A traceback occurs. This is due to python3.7 namespace packages having their __file__ attribute set to None.

Reproduction steps

Instead of running the commands below, you can also extract the attached zip file and run app.py.

mkdir -p foo/bar
mkdir foo/bar/templates
echo 'x = 1' > foo/bar/baz.py
echo 'Hi' > foo/bar/templates/test.html
cat << 'EOF' > app.py
#!/usr/bin/env python3

import jinja2
jinja_env = jinja2.Environment(loader=jinja2.PackageLoader('foo.bar', 'templates'))
print(jinja_env.get_template('test.html').render())
EOF
chmod +x app.py
./app.py

Full Traceback

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

Your Environment

  • Python version: 3.7.4
  • Jinja version: 2.10.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions