New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FileSystemLoader doesn't work with Windows paths (backslashes) #767
Comments
Jinja template names are not fileystem paths (even though they map to filesystem paths when using just a FileSystemLoader). They always use forward slashes so this is working as intended. |
I think this information should be explicitly stated in the documentation with bold font if you don't intend to fix it. It's not that I have purposefully used backslashes - when generating the filename using some other python code (for example by searching the folders for appropriate extensions), the string has system's native slashes (so backslashes on Windows). In that case you have to explicitly replace the slashes... BTW - I'm not using Windows, but I'm writing a script which should work on all platforms. |
The fixes are related to the fact that both jinja ( pallets/jinja#767 ) and templates expect forward slashes only. 1. Use posixpath instead of os.path 2. Replace all backslashes with slashes in current folder returned by os.walk()
Jinja always requires template paths to use forward slashes. See pallets/jinja#711 (comment) and pallets/jinja#767 (comment) for more details. Refs CoastalResilienceNetwork/geosite-framework-build#37
Jinja always requires template paths to use forward slashes. See pallets/jinja#711 (comment) and pallets/jinja#767 (comment) for more details. Refs CoastalResilienceNetwork/geosite-framework-build#37
Jinja always requires template paths to use forward slashes. See pallets/jinja#711 (comment) and pallets/jinja#767 (comment) for more details. Refs CoastalResilienceNetwork/geosite-framework-build#37
@FreddieChopin What I have to do is to manually replace all |
"It is not a bug - it's a feature!" (; |
This is a really weird one! Could this be changed or mentioned (in big letters) in the docs please? |
PR welcome (for the docs) |
I started on one, but then I found this: https://github.com/pallets/jinja/blob/master/jinja2/loaders.py#L43-L61
The docs explicitly say that os.path.join should/could be used. Perhaps an issue is more appropriate? |
The real problem is python path module returns path with '\' on windows by default. I know it's right behaviour somehow but built-in function like "open" accepts both slash and backslash so "sometimes it works, sometimes it doesn't" kind of things happens. Probably we need some way to treat paths independent from platforms...some sort of "python convention to treat a paths internally" sort of things...I think this is not a problem of jinja2 (This is a problem of historical convention between Unix and Windows and it seems last forever) but some "heads up" in a starter documents are welcomed. |
The fundamental problem is that a template "path" is not really an OS path, but is expected to contain only "/" separators. But in the loaders.get_source(), self.searchpath also thinks that the path separator is always "/". The template paths should not be changed to be actual OS paths, because there are apparently other places where this canonical assumption is used. The solution is to change both the template and the searchpath to contain actual OS path separators, at the point where OS paths are needed:
I have tested this code briefly in Windows 10/Python 2.7, and it corrected my "Template not found" error. |
Expected Behavior
On Windows
FileSystemLoader
should allow both Windows-style (with\
) and UNIX-style (with/
) paths.Actual Behavior
On Windows, when using
FileSystemLoader
the following fails:jinjaEnvironment.get_template('.\source\architecture\ARM\ARMv6-M-ARMv7-M\bo ardTemplates\ARMv6-M-ARMv7-M.metadata')
with
jinja2.exceptions.TemplateNotFound: .\source\architecture\ARM\ARMv6-M-ARMv7-M\bo ardTemplates\ARMv6-M-ARMv7-M.metadata
while this
jinjaEnvironment.get_template('.\source\architecture\ARM\ARMv6-M-ARMv7-M\bo ardTemplates\ARMv6-M-ARMv7-M.metadata'.replace('\\', '/'))
works perfectly fine.Full Traceback
Your Environment
The text was updated successfully, but these errors were encountered: