You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I guess this is the first thread about Windows in the Jinja GitHub discussions, weird!
This pertains to issue #711, which was closed by a project member in 2017. It seems unlikely to be resolved without a PR from a volunteer. Here's a workaround that (crudely) avoids the jinja2.exceptions.TemplateNotFound you'd otherwise get on Windows platforms if you feed a path generated by os.path.join to a Jinja FileSystemLoader.
Background
I'm using Jinja's FileSystemLoader for a project as a quick-and-dirty static site generator, where I'm iterating over the entire directory structure under the template directory and reproducing it with the processed templates in a parallel, destination directory. This project must work cross-platform.
One issue that came up is, as stated in the docs“/” is used as the path separator, even on Windows, which creates difficulties if you're using os.path.join or os.path.relpath.
Although modern Windows seems to accept either forward or backslashes, the os.path utilities insert the platform's native path separator: "\". However, FileSystemLoader.get_source calls split_template_path which only splits on /s, thus it crashes with a traceback similar to the following:
I understand why—there are ModuleLoaders and PackageLoaders, and someone had to pick a separator1—but in this particular context, the behavior defies the principle of least astonishment. Importing posixpath explicitly as @ThiefMaster mentioned here is one possible workaround, with the downside being that that requires modifying code far removed from the actual template processing.
Workaround
Here's a crude workaround that reconciles the path separators right at the nexus. A more robust solution might split and then re-join using posixpath.join, but this was sufficient for my purposes, so I didn't think it through.
fromjinjaimportEnvironment, FileSystemLoaderTEMPLATE_DIR='templates'classBackslashFriendlyFileSystemLoader(FileSystemLoader):
""" Work around pallets/jinja issue #711, which pertains only to Windows. Jinja "template paths are not necessarily filesystem paths and […] use forward slashes," so just do a crude subsitution and hope for the best ref. https://github.com/pallets/jinja/issues/711#issuecomment-300070379 and https://github.com/pallets/jinja/blob/3.1.6/src/jinja2/loaders.py#L194 """defget_source(self, environment, template):
template=template.replace('\\', '/')
returnsuper().get_source(environment, template)
env=Environment(loader=BackSlashFriendlyFileSystemLoader(TEMPLATE_DIR))
Hope that helps somebody.
Footnotes
Also to avoid "drive:" or UNC segments breaking out of the search directory as mentioned here↩
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I guess this is the first thread about Windows in the Jinja GitHub discussions, weird!
This pertains to issue #711, which was closed by a project member in 2017. It seems unlikely to be resolved without a PR from a volunteer. Here's a workaround that (crudely) avoids the
jinja2.exceptions.TemplateNotFoundyou'd otherwise get on Windows platforms if you feed a path generated byos.path.jointo a JinjaFileSystemLoader.Background
I'm using Jinja's
FileSystemLoaderfor a project as a quick-and-dirty static site generator, where I'm iterating over the entire directory structure under the template directory and reproducing it with the processed templates in a parallel, destination directory. This project must work cross-platform.One issue that came up is, as stated in the docs , which creates difficulties if you're using
os.path.joinoros.path.relpath.Although modern Windows seems to accept either forward or backslashes, the
os.pathutilities insert the platform's native path separator: "\". However,FileSystemLoader.get_sourcecallssplit_template_pathwhich only splits on/s, thus it crashes with a traceback similar to the following:I understand why—there are
ModuleLoaders andPackageLoaders, and someone had to pick a separator1—but in this particular context, the behavior defies the principle of least astonishment. Importingposixpathexplicitly as @ThiefMaster mentioned here is one possible workaround, with the downside being that that requires modifying code far removed from the actual template processing.Workaround
Here's a crude workaround that reconciles the path separators right at the nexus. A more robust solution might split and then re-join using
posixpath.join, but this was sufficient for my purposes, so I didn't think it through.Hope that helps somebody.
Footnotes
Also as mentioned here ↩
Beta Was this translation helpful? Give feedback.
All reactions