I use the example provided in the documentation at https://jinja.palletsprojects.com/en/3.0.x/api/#basics
The file yourapp.py contains:
#!/usr/bin/env python3
from jinja2 import Environment, PackageLoader, select_autoescape
env = Environment(
loader=PackageLoader("yourapp"),
autoescape=select_autoescape()
)
I get:
$ python3 yourapp.py
Traceback (most recent call last):
File "/private/tmp/test/yourapp.py", line 5, in <module>
loader=PackageLoader("yourapp"),
File "/usr/local/lib/python3.9/site-packages/jinja2/loaders.py", line 287, in __init__
import_module(package_name)
File "/usr/local/Cellar/python@3.9/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/private/tmp/test/yourapp.py", line 5, in <module>
loader=PackageLoader("yourapp"),
File "/usr/local/lib/python3.9/site-packages/jinja2/loaders.py", line 311, in __init__
raise ValueError(
ValueError: The 'yourapp' package was not installed in a way that PackageLoader understands.
The problem looks like because yourapp.py is not a package but just a (simple) Python script.
In this case spec.submodule_search_locations is None.
$ python3
Python 3.9.7 (default, Sep 3 2021, 12:37:55)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib.util
>>> package_name='yourapp'
>>> spec = importlib.util.find_spec(package_name)
>>> print(spec.submodule_search_locations)
None
Maybe the loader could search in the current directory?
The same code was working fine with version 2.11.2 of Jinja2 for example.
I found related issues in #1148 and #1168 but I could not find a (simple) solution to my problem.
Proposed patch:
--- loaders.py.orig 2021-10-13 21:20:16.000000000 +0200
+++ loaders.py 2021-10-13 21:21:04.000000000 +0200
@@ -306,6 +306,8 @@
if os.path.isdir(root):
template_root = root
break
+ else:
+ template_root = 'templates'
if template_root is None:
raise ValueError(
This patch works for me.
Environment:
- Python version: 3.9.7
- Jinja version: 3.0.2
I use the example provided in the documentation at https://jinja.palletsprojects.com/en/3.0.x/api/#basics
The file
yourapp.pycontains:I get:
The problem looks like because
yourapp.pyis not a package but just a (simple) Python script.In this case
spec.submodule_search_locationsis None.Maybe the loader could search in the current directory?
The same code was working fine with version 2.11.2 of Jinja2 for example.
I found related issues in #1148 and #1168 but I could not find a (simple) solution to my problem.
Proposed patch:
This patch works for me.
Environment: