[mod_python] Why this code fails?

Graham Dumpleton grahamd at dscpl.com.au
Mon Nov 20 17:21:05 EST 2006


=?iso-8859-1?Q?Luis_M._Gonz=E1lez?= wrote ..
> Thank you Graham!
> I will follow your suggestions to se how it goes...
> 
> By the way, it's worth mentioning that, for the sake of simplicity, I also
> tried placing my "models.py" module within the same directory of my 
> "index.py" script, in order to use a simple import statement (without 
> messing with sys.path and os) but the problem persists.
> 
> However, if I import my ORM into index.py and then I define my models there,
> it works.
> This is strange: why can I import the ORM script but not the models script?
> Is this because of the problem with the module importer you talked about?

It may not find the 'models.py' file in the same directory as the 'index.py'
file for a number of reasons.

First is that PythonPath is specified for that context meaning that mod_python
isn't adding the handler root directory where index.py is into sys.path
automatically.

Second is that 'index.py' wasn't in the handler root directory but a subdirectory.

Third is that there is a 'models' module elsewhere and the order of sys.path
is such that it finds this other one instead of that in the same directory.

For the record, all three of these problems are fixed in mod_python 3.3
and will work as one would expect it to work for file based modules like
'models.py'.

Graham

> --- Original Message ----- 
> From: "Graham Dumpleton" <grahamd at dscpl.com.au>
> To: "Luis M. González" <luismg at gmx.net>
> Cc: <mod_python at modpython.org>
> Sent: Monday, November 20, 2006 12:20 AM
> Subject: Re: [mod_python] Why this code fails?
> 
> 
> > =?iso-8859-1?Q?Luis_M._Gonz=E1lez?= wrote ..
> >> Hello all,
> >>
> >> I have this script, which works fine when tested with the interpreter,
> >> but fails when used with mod_python:
> >>
> >> # index.py
> >>
> >> import sys, os
> >> sys.path.append(os.getcwd() + '/templates')
> >> sys.path.append(os.getcwd() + '/models')
> >
> > You shouldn't modify sys.path from inside a handler code file as it can
> > cause lots of problems. Also, os.getcwd() is going to usually return
> '/'
> > and not the directory the file resides in. It is also not a good idea
> to 
> > be
> > placing modules required by your handlers within the document tree
> > like this because if you do not protect them properly from access in
> > the Apache configuration, users may be able to trigger code or access
> > data within the modules directly.
> >
> > The better thing to do is to move those module directories somewhere
> > outside of the document tree and then use the PythonPath directive to
> > extend the value of sys.path with the directory you place the two module
> > directories in.
> >
> > Also note there are various problems with the module importer in
> > current versions of mod_python. See:
> >
> >  http://www.dscpl.com.au/wiki/ModPython/Articles/ModuleImportingIsBroken
> >
> > This could be resulting in you picking up a wrong version of a module
> > if the same module name is used in multiple places, or an old module
> > version cached in memory and not changes you may have made on disk
> > since it was loaded. You may as a result have to restart Apache if not
> > getting latest module changes. You may also need to use PythonInterpreter
> > to make sure distinct applications operate in their own interpreters
> to
> > avoid problems with similar module names being used.
> >
> > Graham
> >
> >> from models import *
> >>
> >> result = Customers.select()
> >>
> >> def index(req):
> >>     req.content_type = 'text/html'
> >>     for i in result:
> >>         req.write(i.Company + '<br>')
> >>
> >>
> >> Explanation:
> >> I have a main directory and two subdirectories: templates and models.
> >> Inside the models folder I have a module called "models.py", where I
> >> imported an ORM and defined the db connection and models (included
> >> "Customers").
> >>
> >> Although it works perfectly as a standalone app, it doesn't work with
> >> mod_python.
> >> The error message I get is:
> >>
> >>              NameError: name 'Clientes' is not defined
> >>
> >> I guess it has to do with the way mod_python handles the import of
> >> modules, or perhaps with the way I modify the sys.path... but I can't
> >> figure out the solution.
> >>
> >> Any hint would be highly appreciated...
> >> Thanks!
> >> Luis
> > 


More information about the Mod_python mailing list