[mod_python] mod_python claims it cannot find a module

Graham Dumpleton grahamd at dscpl.com.au
Fri Nov 11 06:43:09 EST 2005


On 11/11/2005, at 9:45 PM, Martin MOKREJŠ wrote:

> 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.

When using:

   from sql_io import cField

the whole file is still imported. The only difference is that references
are added into the globals of the caller only for the items listed.

Thus, presuming the sql_io module is in that same directory, the
following is equivalent:

   sql_io = apache.import_module("sql_io",path=[_directory])
   cField = sql_io.cField

Graham

> 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