Syntax error in jinja 2 library

2019-02-27 22:37发布

问题:

For running the IPython Notebook on a suse Linux server, I needed to install the jinja2 library:

pip-3.2 install jinja2

Installation printed a syntax error but also said "installation successful".

On import, I get the following error:

In [1]: import jinja2
  File "/usr/local/lib/python3.2/site-packages/jinja2/environment.py", line 639
    u'\xff\xff\xff\xff'.encode('iso-8859-15')
                      ^
SyntaxError: invalid syntax

Bug in the jinja2 package? Any way for me to fix this?

回答1:

Jinja2 only supports Python 3.3 and up, you are trying to install it for Python 3.2. Quoting from the documentation:

Jinja 2.7 brings experimental support for Python >=3.3.

Python 3.3 added support for u'..' string literals to make it easier to write compatible code that runs both on Python 2 and 3, which Jinja2 makes use of.

You'll either have to upgrade to Python 3.3, or pick a different templating library or use an earlier version.

The requirement was upgraded to 3.3 in version 2.7 (see the changelog), so you could try installing 2.6:

pip install jinja2==2.6


回答2:

Install jinja 2.5, using:

easy_install jinja2==2.5

That solved the problem for me (happily).