[mod_python] database connection

Graham Dumpleton grahamd at dscpl.com.au
Sun Oct 16 06:26:04 EDT 2005


On 16/10/2005, at 7:42 PM, Julien Cigar wrote:

> Graham Dumpleton wrote:
>
>> A comment on something else in your example first.
>>
>>> pageAction1 = apache.import_module('pages/action1')
>>
>>
>> I wouldn't rely rely on being able to specific '/' in the module name 
>> as
>> argument. The documentation only mentions that '.' can be used if
>> needing to reference a subcomponent of a package. What you are doing 
>> is
>> relying on a strange quirk of the implementation and in mod_python 3.3
>> if the module loading system is overhauled there may be no guarantee
>> that it would work the same. You could always campaign though that it
>> is a useful future that should be maintained but documented. :-)
>>
>> BTW, does the "pages" directory actually have an "__init__.py" file. I
>> think what you are doing doesn't require it to, but wanted to check if
>> you do have it or not.
>>
> My directory structure looks like this :
> index.py
> config.cfg
> config.py
> pages/page1.py
> pages/page2.py
> pages/page3.py
> ...
>
> My "pages" directory doesn't contain an __init__.py file (should I ?)
> I don't understand what's wrong with 
> apache.import_module(pages/page1.py) .. ? I always done like this :)
> Is it specific to mod_python ? I mean can you import pages/page1.py 
> with the classic Pyhon import ?

You can't use '/' when using the "import" statement in classic Python.
Ie., can't do:

   import pages/page1

You can use '.', ie.,

   import pages.page1

But then this is a package import and the "pages" directory needs to be
set up as a package, ie., it must contain an "__init__.py" file in it,
even if "__init__.py" has nothing in it.

Similarly, if you use the __import__ builtin function, you can't use 
'/',
ie., can't do:

   page1 = __import__("pages/page1")

but can do:

   page1 = __import__("pages.page1")

In other words, that you can specify a '/' like you are is quite 
specific
to the current mod_python implementation of apache.import_module(). 
Well,
that is what I thought ......

At this point I am a bit confused as I can't find anything in the 
mod_python
code which would even allow '/' to work. When I even try using it on my
platform, Python throws an exception:

   ImportError: No module named pages/page1

What version of mod_python are you using, what version of Python and 
what
platform? I don't understand how it could be working for you.

Graham



More information about the Mod_python mailing list