SyntaxError: from __future__ imports in Cheetah
Monday, 3. September 2007, 10:25:19
What is that Cheetah in your computer:
Many PHP programmers are already familiar with templates, and most of them (you?) probably use Smarty which is a really good templating system for PHP. Cheetah is probably what comes the closest to Smarty when Python is your language of choice. The syntax and logic seems familiar right away, but when it comes to possibilities and speed, I will probably let Cheetah walk off with an extra start.
You said you had a problem with it:
Although they claim on their web page that the 'RC' in the release name is only there to show that the documentation is yet to be updated, this is not the whole truth. As far as I can see all Release Candidates from 1 through 8 has core changes that might affect you in one way or an other. At the same time, there is not a native Windows build for every Release Candidate either, so the newest Windows build is b4 while the newest tarball is RC8, which again will of course give you some problems if you do it the lazy way I did while installing. But the problem is not hard to fix, and it is not only Cheetah having this problem these days. The error output I got while running the first Cheetah test looked like this:
Traceback (most recent call last):
File "error.py", line 3, in <module>
from Cheetah.Template import Template
File "C:\Program Files\python25\lib\site-packages\Cheetah\Template.py", line 48, in <module>
from Cheetah.Compiler import Compiler
File "C:\Program Files\python25\lib\site-packages\Cheetah\Compiler.py", line 33, in <module>
from Cheetah.Parser import (
File "C:\Program Files\python25\lib\site-packages\Cheetah\Parser.py", line 32, in <module>
from Cheetah import ErrorCatchers
File "C:\Program Files\python25\lib\site-packages\Cheetah\ErrorCatchers.py", line 16, in <module>
from Cheetah.NameMapper import NotFound
File "C:\Program Files\python25\lib\site-packages\Cheetah\NameMapper.py", line 146
from __future__ import generators
SyntaxError: from __future__ imports must occur at the beginning of the file
This time the problem was in the NameMapper.py file which Cheetah uses. But I have seen the same problem in Client.py from SOAPpy, and admin.py from Trac and many more. There is usually a really easy fix to this problem. Though I am not 100% I think it is the new import rules in Python 2.5 that makes this a problem. The simpler parts of PEP 328 (Absolute and Relative Imports) was implemented in Python 2.4, and the more complicated parts where implemented in Python 2.5. But since the plan is to move toward making absolute imports the default in future versions of Python, this does not seem like a bug, but rather a feature that won't go away in further releases, so if you are a module owner and get this run error, I suggest you fix it sooner than later.
__future__ is a special module used to change the behaviour of the parser, so it is extremely important that it occur in the beginning of your module. Earlier it was fine to add __author__ or __copyright__ constants on the top of your module, but this is not the way to do it anymore. Just move the imports to the top of your code, and that's all there is to it. As an example I have copied the part of the old version of NameMapper.py that needs the fix:
Authors: Tavis Rudd,
Chuck Esterbrook
Version: $Revision: 1.28 $
Start Date: 2001/04/03
Last Revision Date: $Date: 2005/01/03 19:05:49 $
"""
__author__ = "Tavis Rudd ," +\
"\nChuck Esterbrook "
__revision__ = "$Revision: 1.28 $"[11:-2]
from __future__ import generators
import types
from types import StringType, InstanceType, ClassType, TypeType
from pprint import pformat
import inspect
_INCLUDE_NAMESPACE_REPR_IN_NOTFOUND_EXCEPTIONS = False
_ALLOW_WRAPPING_OF_NOTFOUND_EXCEPTIONS = True
And here you have the same part of the NameMapper.py file after moving the imports above the variable declarations. There is not more to it than that, but there is a lot of modules out there that needs the fix, included my own modules. I guess Cheetah already have addressed this in the latest RCs, but I just wanted to make a point out of it since it seems to pop up everywhere this long after 2.5 was released.
Authors: Tavis Rudd ,
Chuck Esterbrook
Version: $Revision: 1.28 $
Start Date: 2001/04/03
Last Revision Date: $Date: 2005/01/03 19:05:49 $
"""
from __future__ import generators
import types
from types import StringType, InstanceType, ClassType, TypeType
from pprint import pformat
import inspect
__author__ = "Tavis Rudd ," +\
"\nChuck Esterbrook "
__revision__ = "$Revision: 1.28 $"[11:-2]
_INCLUDE_NAMESPACE_REPR_IN_NOTFOUND_EXCEPTIONS = False
_ALLOW_WRAPPING_OF_NOTFOUND_EXCEPTIONS = True
- Øyvind Østlund -
By Aneeqa, # 4. September 2007, 17:38:54
By keshav0001, # 26. September 2007, 12:56:53
By Debih, # 8. October 2007, 20:27:34