[mod_python] reloading issue

Graham Dumpleton graham.dumpleton at gmail.com
Wed Mar 14 00:54:30 EST 2007


On 14/03/07, me <mlists at e-beyond.de> wrote:
> Hello Graham,
>
> Am Dienstag 13 März 2007 22:22:38 schrieben Sie:
> > > The Factory:
> > > class ContentFactory:
> > >   def factory(self,className, *args):
> > >     aClass = getattr(__import__(__name__), className)
> >
> > Why are you using __name__ as argument to __import__ here as that
> > would mean you are trying to import the same module as the code is in,
> > although that will not actually work as you think as modules like your
> > index.py file are imported by special mod_python module importer and
> > __name__ will not be 'index' but  in this case
> > '_mp_977643700f732f7a07a576608a2136c2'. Since the mod_python module
> > importer will not find a .py file by that name, it will fallback to
> > Python importer which I would have though would have failed to find
> > it.
> >
> > Are you really wanting to import the same module as the code is in?
> >
> > Rather than using __import__() you might be better off looking at the
> > apache.import_module() function as described in:
> >
> >   http://www.modpython.org/live/current/doc-html/pyapi-apmeth.html
> >
> > This will give you better control over module importing as file can be
> > specified by full path. Also allows automatic module reloading.
> >
>
> I'm fairly new to python (I've used python years ago and came back to use it
> now) and I was trying to implement an factory. So I can load Classes based
> on a provided string and not to use a huge if elif condition.
>
> If I use the apache.import_module it should look like the following, right?
>
> def factory(self,className, *args):
>         module = apache.import_module('./Contents/'+className+'.py')
>         aClass = getattr(module, className)
>
> The strange thing with the 'old' factory: it's working. The strange behaviour
> that this error occurs sometimes is gone after 3-4 apache restarts. Now it
> works as expected. (Okay I'll reimplement the factory now)

Presuming that the 'Contents' directory is a subdirectory of the
directory where the code file containing that factory method is
contained in, and the directory the factory method is contained in is
a module imported by mod_python or mod_python.publisher, then yes it
should work.

If you have defined use of mod_python.publisher for the directory the
factory method is in, all those files in the 'Contents' subdirectory
will also be visible through mod_python.publisher. You should
therefore move your 'Contents' directory out of the web server
document tree to avoid them being called directly, or put a .htaccess
file in the 'Contents' directory which contains:

  Deny from all

This will stop them being accessible by mod_python.publisher from a client.

Graham



More information about the Mod_python mailing list