[mod_python] Apache1.3.33 + Python2.3.4 + mod_python - Can't get Python templates to work

Nicolas Lehuen nicolas.lehuen at gmail.com
Tue Jan 25 11:12:54 EST 2005


I think the problem is the CWD, which is automatically set to / IIRC.
You have to give an absolute path to your template files. The best way
to do this is to use the __file__ variable, which gives you the
absolute path of the current .py file, and navigate from this file
using the os and os.path modules.

Regards,
Nicolas


On Tue, 25 Jan 2005 07:52:42 -0800, Brandon Masterson
<bmasters at direcway.com> wrote:
>  I'm a perl templates user trying to learn Python and templates but I've run
> into a brick wall with the inability to open any of the template files. 
> I've tried Cheetah, Simpletal, and htmltmpl all with the same basic problem
> so I suspect I've got something wrong with either my apache or mod_python
> setup.  The problem is, when I attempt to run the .py files thru the
> browser, the scripts get down to the open command and then can't find the
> template files.  I can run scripts that don't have template accesses just
> fine but attempts to open local directory files causes the errors below.
> Also when I run the scripts calling templates on the command line with the
> interpreter, they work fine.  One other thing I tried was to add the path to
> the template names in the open commands but this resulted in a slightly
> different error (see bottom of this email).
> 
>  For reference this is a Debian machine.  Here's the module versions
> installed:
>  root at Knoppix:/var/www/pythontest# apt-show-versions | sort | egrep
> "apache|python"
>  apache-common/testing uptodate 1.3.33-2
>  apache-utils/testing uptodate 1.3.33-2
>  apache/testing uptodate 1.3.33-2
>  apache2-common/testing uptodate 2.0.52-3
>  apache2-doc/testing uptodate 2.0.52-3
>  apache2-mpm-worker/testing uptodate 2.0.52-3
>  libapache-mod-python/testing uptodate 2:2.7.10-3
>  libapache-mod-python2.3/testing uptodate 2:2.7.10-3
>  libapache-mod-ssl/testing uptodate 2.8.22-1
>  libapache2-svn/testing uptodate 1.1.1-2
>  libboost-python1.31.0/testing uptodate 1.31.0-9
>  python-cheetah/testing uptodate 0.9.15-5
>  python-clearsilver/unstable upgradeable from 0.9.8-3 to 0.9.13-1
>  python-dev/testing uptodate 2.3.4-5
>  python-examples/testing uptodate 2.3.4-5
>  python-mysqldb/testing uptodate 1.1.6-1
>  python-sqlite/testing upgradeable from 1.0-2 to 1.0.1-1
>  python-tk/testing upgradeable from 2.3.4-4 to 2.3.4-5
>  python/testing upgradeable from 2.3.4-4 to 2.3.4-5
>  python2.2-xml/testing upgradeable from 0.8.3-5 to 0.8.4-1
>  python2.2-xmlbase/testing upgradeable from 2.2.3-11 to 2.2.3-12
>  python2.2/testing upgradeable from 2.2.3-11 to 2.2.3-12
>  python2.3-cheetah/testing uptodate 0.9.15-5
>  python2.3-clearsilver/unstable upgradeable from 0.9.8-3 to 0.9.13-1
>  python2.3-dev/testing uptodate 2.3.4-19
>  python2.3-examples/testing upgradeable from 2.3.4-18 to 2.3.4-19
>  python2.3-htmlgen/testing uptodate 2.2.2-10
>  python2.3-htmltmpl/testing uptodate 1.22-5
>  python2.3-mysqldb/testing uptodate 1.1.6-1
>  python2.3-sqlite/testing upgradeable from 1.0-2 to 1.0.1-1
>  python2.3-subversion/testing uptodate 1.1.1-2
>  python2.3-tk/testing uptodate 2.3.4-19
>  python2.3-zopeinterface/testing uptodate 3.0.0-2
>  python2.3/testing uptodate 2.3.4-19
> 
>  Any help would be appreciated.
> 
>  SIMPLETAL ATTEMPT:
>  from simpletal import simpleTAL, simpleTALES
>  import sys
>  import os
>  templateFile = open ("basic.html", 'r')
>  templateFile.close()
> 
>  ERROR:
>  Mod_python error: "PythonHandler mod_python.publisher"
> 
>  Traceback (most recent call last):
> 
>    File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 193,
> in Dispatch
>      result = object(req)
> 
>    File "/usr/lib/python2.3/site-packages/mod_python/publisher.py", line
> 132, in handler
>      module = apache.import_module(module_name, _req, [path])
> 
>    File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 335,
> in import_module
>      module = imp.load_module(mname, f, p, d)
> 
>    File "/var/www/pythontest/a.py", line 6, in ?
>      templateFile = open ("basic.html", 'r')
> 
>  IOError: [Errno 2] No such file or directory: 'basic.html'
> 
>  
>  CHEETAH ATTEMPT:
>  from Cheetah.Template import Template
>  templateFileName = 'cheetah.tmpl'
>  t = Template(file=templateFileName, searchList=[{'records':[]},])
> 
>  ERROR:
>  Mod_python error: "PythonHandler mod_python.publisher"
> 
>  Traceback (most recent call last):
> 
>    File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 193,
> in Dispatch
>      result = object(req)
> 
>    File "/usr/lib/python2.3/site-packages/mod_python/publisher.py", line
> 132, in handler
>      module = apache.import_module(module_name, _req, [path])
> 
>    File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 335,
> in import_module
>      module = imp.load_module(mname, f, p, d)
> 
>    File "/var/www/pythontest/b.py", line 6, in ?
>      t = Template(file=templateFileName, searchList=[{'records':[]},])
> 
>    File "/usr/lib/python2.3/site-packages/Cheetah/Template.py", line 156, in
> __init__
>      self.compile(source, file)
> 
>    File "/usr/lib/python2.3/site-packages/Cheetah/Template.py", line 235, in
> compile
>      self._fileMtime = os.path.getmtime(file)
> 
>    File "/usr/lib/python2.3/posixpath.py", line 146, in getmtime
>      return os.stat(filename).st_mtime
> 
>  OSError: [Errno 2] No such file or directory: '/cheetah.tmpl'
> 
>  
> 
>  HTMLTMPL ATTEMPT:
>  from htmltmpl import TemplateManager, TemplateProcessor
>  template = TemplateManager().prepare("template.tmpl")
> 
>  ERROR:
>  Mod_python error: "PythonHandler mod_python.publisher"
> 
>  Traceback (most recent call last):
> 
>    File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 193,
> in Dispatch
>      result = object(req)
> 
>    File "/usr/lib/python2.3/site-packages/mod_python/publisher.py", line
> 132, in handler
>      module = apache.import_module(module_name, _req, [path])
> 
>    File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 335,
> in import_module
>      module = imp.load_module(mname, f, p, d)
> 
>    File "/var/www/pythontest/c.py", line 2, in ?
>      template = TemplateManager().prepare("template.tmpl")
> 
>    File "/usr/lib/python2.3/site-packages/htmltmpl.py", line 222, in prepare
>      compiled = self.compile(file)
> 
>    File "/usr/lib/python2.3/site-packages/htmltmpl.py", line 296, in compile
>      self._debug).compile(file)
> 
>    File "/usr/lib/python2.3/site-packages/htmltmpl.py", line 988, in compile
>      tokens = self.parse(self.read(file))
> 
>    File "/usr/lib/python2.3/site-packages/htmltmpl.py", line 1038, in read
>      raise TemplateError, "IO error while reading template '%s': "\
> 
>  TemplateError: Htmltmpl error: IO error while reading template
> 'template.tmpl': (2) No such file or directory
> 
>  
>  FILES:
>  -rw-r--r--  1 www-data www-data  343 Jan 25 08:42 c.pyc
>  -rwxr-xr-x  1 root     root      111 Jan 25 08:42 c.py
>  -rw-r--r--  1 www-data www-data  332 Jan 25 08:36 b.pyc
>  -rw-r--r--  1 www-data www-data  387 Jan 25 08:34 a.pyc
>  -rw-r--r--  1 root     root      390 Jan 25 08:33 cheetah.tmpl
>  -rw-r--r--  1 root     root      203 Jan 25 08:31 b.py
>  -rwxr-xr-x  1 root     root      152 Jan 25 08:31 a.py
>  -rw-r--r--  1 www-data www-data  882 Jan 24 23:40 template.tmplc
>  -rw-r--r--  1 root     root      702 Jan 24 23:22 template.tmpl
>  -rw-r--r--  1 root     root      239 Jan 23 18:49 basic.html
> 
>  APACHE CONFIG (PYTHON SPECIFIC):
>  AddType text/plain .py
>  <Directory /var/www/pythontest>
>       Options -Indexes FollowSymLinks MultiViews
>       AllowOverride None
> 
>       AddHandler python-program .py
>       PythonHandler mod_python.publisher
>       PythonDebug On
> 
>  </Directory>
> 
>  
> 
>  ERROR MESSAGES WITH FULL PATH (/var/www/pythontest/c.py) SPECIFIED:
>  Not Found
>  The requested URL /pythontest/c.py was not found on this server.
>  Apache/1.3.33 Server at 192.168.0.12 Port 80
> 
>  
>  What am I missing?
> 
>  Thanks...
> 
>  Brandon
>  
>  
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
> 
> 
>


More information about the Mod_python mailing list