[mod_python] mod_python claims it cannot find a module

Nicolas Lehuen nicolas.lehuen at gmail.com
Tue Jul 5 02:11:27 EDT 2005


Thanks Graham, this was getting a little bit tricky for me...

AFAIK the publisher doesn't try to import index.py if importing the 
requested file fails. However, at one point I'm testing whether the 
requested file exists or not like this :

####################### The publisher handler himself 
########################## 

def handler(req):

 req.allow_methods(["GET", "POST", "HEAD"])
 if req.method not in ["GET", "POST", "HEAD"]:
 raise apache.SERVER_RETURN, apache.HTTP_METHOD_NOT_ALLOWED

 # if the file exists, req.finfo is not None
 if req.finfo:
 
 # The file exists, so we have a request of the form :
 # /directory/[module][/func_path]
 
 # we check whether there is a file name or not
 path, filename = split(req.filename)
 if not filename:
 
 # if not, we look for index.py
 req.filename = join(path, 'index.py')

 # Now we build the function path
 if not req.path_info or req.path_info=='/':

 # we don't have a path info, or it's just a slash,
 # so we'll call index
 func_path = 'index'

 else:

 # we have a path_info, so we use it, removing the first slash
 func_path = req.path_info[1:]
 
 else:
 
 # First we check if there is a Python module with that name
 # just by adding a .py extension
 if isfile(req.filename+'.py'):

 req.filename += '.py'
 
 # Now we build the function path
 if not req.path_info or req.path_info=='/':
 
 # we don't have a path info, or it's just a slash,
 # so we'll call index
 func_path = 'index'
 
 else:
 
 # we have a path_info, so we use it, removing the first slash
 func_path = req.path_info[1:]
 else:

 # The file does not exist, so it seems we are in the
 # case of a request in the form :
 # /directory/func_path
 
 # we'll just insert the module name index.py in the middle
 path, func_path = split(req.filename)
 req.filename = join(path, 'index.py')
 
 # I don't know if it's still possible to have a path_info
 # but if we have one, we append it to the filename which
 # is considered as a path_info.
 if req.path_info:
 func_path = func_path + req.path_info

What I suspect in Martin's case is that req.finfo returns None because of 
the global scope of his PythonHandler configuration, or something like that. 
Therefore, the publisher ends up executing the last branch which splits the 
requested file name into directory + func_path and adds index.py to the 
directory. I'll try to reproduce this behaviour and let you know about this.

Regards,
Nicolas

2005/7/5, Graham Dumpleton <grahamd at dscpl.com.au>:
> 
> =?windows-1252?Q?Martin_MOKREJ=8A?= wrote ..
> > > Because of problems with mixing "import" with the mod_python module
> > > loader, you are actually better of using:
> > >
> > > from mod_python import apache
> > > import os
> > >
> > > directory = os.path.dirname(__file__)
> > >
> > > web_settings = apache.import_module("web_settings",path=[directory])
> > >
> > > Here you are explicitly telling it which directory to get the module
> > > from and avoid all problems with things not being found in the Python
> > > module search path. You should only use this for loading in your own
> > > modules though, not standard Python modules.
> >
> > Graham, how can I fork the code so that for normal "console" use it 
> would
> > do
> > normal import and that when running under mod_apache it would run this
> > trick?
> > I use the modules also for command-line tests and some utilities.
> > What variable should I look for in __dict__.keys()? ;-)
> 
> You could use:
> 
> try:
> from mod_python import apache
> directory = os.path.dirname(__file__)
> web_settings = apache.import_module("web_settings",path=[directory])
> except:
> import web_settings
> 
> The import of "apache" from a command line script will fail and thus
> it will fall through to normal "import" statement for importing 
> web_settings.
> 
> There are cleaner ways, but it gets quite complicated and the mod_python
> module importing system as implemented by apache.import_module() has
> some problems at the moment which makes it even worse. This all might
> get solved in a future version of mod_python and at that point the cleaner
> way may be available.
> 
> Graham
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20050705/df9d4d95/attachment.html


More information about the Mod_python mailing list