[mod_python] __import__ error

Graham Dumpleton grahamd at dscpl.com.au
Wed Jun 14 04:29:37 EDT 2006


On 14/06/2006, at 6:21 PM, Richard Lewis wrote:

> On Tuesday 13 June 2006 23:57, Graham Dumpleton wrote:
>> Richard Lewis wrote ..
>>
>>> On Tuesday 13 June 2006 00:39, Graham Dumpleton wrote:
>>>> Note that using 'os.sep' in module naming hierarchy only works  
>>>> on some
>>>> platforms when using __import__ and thus is not portable. It may  
>>>> work
>>>> on Linux, but doesn't work on Mac OS X. Don't know if it works  
>>>> on Win32
>>>> or not.
>>>
>>> ...only if I continue to use os.sep. I've put it in a try block like
>>> this:
>>>
>>> try:
>>>   self.handler = __import__(module).__dict__[handler]()
>>> except ImportError:
>>>   self.handler = __import__(module.replace(".",
>>> os.sep)).__dict__[handler]()
>>>
>>> but it always uses the exception case. I haven't tried it with OS  
>>> X or
>>> Windows
>>> yet. Perhaps they'll use the first case properly.
>>
>> Where importing "a/b" targeting "a/b.py", is there also a "a/ 
>> __init__.py"?
>>
> No, there isn't.
>
>> In other words, is the directory "a" properly setup as a Python  
>> package
>> with an "__init__.py" file? If not, then '.' notation wouldn't  
>> work but '/'
>> may (at least for that platform).
>>
> I see. I hand't really understood that that was necessary before.
>
> I think I'll try and leave it as it is, though. Because the Python  
> code being
> imported here is a custom, optional user-defined module which  
> implements a
> xml.sax.handler.ContenHandler class. Its in a directory which also  
> contains
> user-defined XSLT stylesheets and is named by the user in the  
> sitemap.xml
> configuration file as a "transformer" for a "pipeline". So its not  
> really a
> package, just an imported class.

Fair enough, just be aware of the portability issue.

FWIW, the new module importer to be included in future version of  
mod_python
will have added extra flexibility as far as defining where modules  
can be
imported from and modules will be able to be specified by full or  
relative
pathnames, a style of use which should make putting modules in a common
directory but avoiding use of an actual package somewhat easier and more
deterministic.

Graham




More information about the Mod_python mailing list