[mod_python] mod_python claims it cannot find a module

Graham Dumpleton grahamd at dscpl.com.au
Mon Jul 4 20:05:05 EDT 2005


=?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


More information about the Mod_python mailing list