Search code examples
python-3.xmako

Mako: cannot import the Template class. Have a SyntaxError error in "\mako\template.py", line 622


I want to try Mako with Django instead of Django's default template language. But I'm having a problem when I try to import Mako's Template class as written in the manual:

from mako.template import Template 
mytemplate = Template("hello world!") 
print mytemplate.render()

I do this in Windows cmd and receive such an error:

C:\Documents and Settings\User>cd C:\py\project\vendor\template\Mako_73  // cd to where I unpacked Mako
C:\py\project\vendor\template\Mako_73>python   // run Python interpreter

>>> from mako.template import Template  // trying to import and getting an error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".\mako\template.py", line 622
    exec code in module.__dict__, module.__dict__
            ^
SyntaxError: invalid syntax

The code from that part:

def _compile_text(template, text, filename):
    identifier = template.module_id
    source, lexer = _compile(template, text, filename,
                        generate_magic_comment=template.disable_unicode)

    cid = identifier
    if not util.py3k and isinstance(cid, unicode):
        cid = cid.encode()
    module = types.ModuleType(cid)
    code = compile(source, cid, 'exec')    
    exec code in module.__dict__, module.__dict__
    return (source, module)

What can it be? I couldn't find anything in Google about this error.

I'm using Python 3.3.

I've downloaded Mako-0.7.3 as tar.gz file and just unzipped it in C:\py\poject\vendor\template\Mako_73. I do not have this directory in the PYTHONPATH or paths.pth. C:\py\poject is a directory where my Django project lives and in \vendor\template I've decided to put Mako and import it from there.

UPD

I found the solution. I've installed the Pyramid Framework and have taken the Mako from there as the Mako is a default template system in it. And Pyramid's version works fine.


Solution

  • Your basic problem is that you are using Python 3, which is relatively new for large projects like Django.

    Secondly, you need to find out how to install packages correctly. I don't know where you got Mako from, but you won't find anywhere that says "just unpack the tarball" - perhaps you are assuming that from your knowledge of PHP, but it's not correct.

    On the Mako site, the suggested method of installation is pip.

    If you go for downloading manually, you need to read instructions about installing Python packages, for example here: http://wiki.python.org/moin/CheeseShopTutorial

    The reason Mako doesn't work for you is that the installation procedure (which you haven't run) converts all the provided Python 2 code so that it works on Python 3. It is not that someone didn't bother to check the code for basic syntax errors!

    If you are trying to use Django, though, Python 3 is definitely the wrong choice - the installation instructions clearly say you need to be using Python 2.5 to 2.7: https://docs.djangoproject.com/en/1.4/intro/install/

    Since you are new to Python, you should try to walk before you run, and go with the tried and tested path - which is Python 2.7 for Django. Ignoring installation instructions and requirements will only slow you down and make life hard.