[mod_python] mod_python claims it cannot find a module

Martin MOKREJŠ mmokrejs at ribosome.natur.cuni.cz
Fri Nov 11 05:45:42 EST 2005


Graham,
  how can I improt just a single funtion from a module (3.2.2b)?
I used to have:

import os
# from cElementTree import parse
from elementtree.ElementTree import parse
import helper_functions 
from sql_io import cField


I'd like to have:

try:
    from mod_python import apache

    _directory = os.path.dirname(__file__)
    for _my_modulename in ['helper_functions']:
        __dict__[_my_modulename] = apache.import_module(_my_modulename, path=[_directory])
except: # without raise
    import helper_functions
    from sql_io import cField # is this possible? I do not want to import the whole file.


TIA
Martin

Graham Dumpleton wrote:
> =?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
> 
> 

-- 
Martin Mokrejs
Email: 'bW9rcmVqc21Acmlib3NvbWUubmF0dXIuY3VuaS5jeg==\n'.decode('base64')
GPG key is at http://www.natur.cuni.cz/~mmokrejs


More information about the Mod_python mailing list