How to fix "Absolute template path names are deprecated."?
I am currently getting the following warning:
/usr/local/lib/python3.10/dist-packages/bottle.py:3383: DeprecationWarning: Absolute template path names are deprecated.
fname = self.search(name, self.lookup)
This seems to be related to the fact that I explicitly set the TEMPLATE_PATH to ensure the template files are found when using a package structure (my template is in my_package/views/testing.jinja2):
from pathlib import Path
import bottle
CURRENT_DIRECTORY = Path(__file__).parent
bottle.TEMPLATE_PATH.append(CURRENT_DIRECTORY / 'views')
@bottle.route("/testing")
@bottle.view("testing.jinja2")
def testing():
return {"key": 42}
The above code is referenced from the my_package/__main__.py file. Running python -m my_package from the parent directory of the package yields the above warning.
Without adding the new template path, browsing to the page yields error code 500 with
Template 'testing.jinja2' not found.
What is the correct way to resolve the templates when using a "real" package structure?
try this and let me know?
import os.path
bottle.TEMPLATE_PATH.append( os.path.relpath( CURRENT_DIRECTORY / 'views'))
This does not seem to work reliably and still raises the deprecation warning. bottle.TEMPLATE_PATH is ['./', './views/', 'brother_ql_web/views'].
It seems like bottle.Jinja2Template.loader resolves the name to /home/me/git/brother_ql_web/brother_ql_web/views/testing.jinja2 and thus https://github.com/bottlepy/bottle/blob/40aec5d4cca6ff4fbd73f4080554580fe4f5c212/bottle.py#L3383 runs into https://github.com/bottlepy/bottle/blob/40aec5d4cca6ff4fbd73f4080554580fe4f5c212/bottle.py#L3283-L3285
Oh, that makes sense. So jinja2 is turing it into an absolute path name and then bottle is complaining. So we don't know why the author decided to depreciate this in 0.12, but that was probably a mistake. Note that the code just complains and then goes ahead and returns an absolute path!!! So I think that this is a bug in bottle.