modules and generated code

Nigel Rantor wiggly at wiggly.org
Tue Nov 14 18:39:57 CET 2006


Peter Otten wrote:
> Nigel Rantor wrote:
> 
>> Peter Otten wrote:
>>> Nigel Rantor wrote:
> 
>>>> So, if I have a tool that generates python code for me (in my case,
>>>> CORBA stubs/skels) in a particular package is there a way of placing my
>>>> own code under the same package hierarchy without all the code living in
>>>> the same directory structure.
> 
>>> http://docs.python.org/lib/module-pkgutil.html
> 
>> Yep, looks like that should work, but it doesn't. :-/
>>
>> Do you have any idea whether other __init__.py scripts from the same
>> logical module will still be run in this case?
> 
> I don't think it will.

Yeah, I am getting that impression. Gah!

>> The generated code uses its init script to pull in other code.
> 
> You could invoke it explicitly via 
> 
> execfile("/path/to/generated/package/__init__.py") 
> 
> in the static package/__init__.py.

Hmm, yes, that works. It's not pretty though, it seems to be finding the 
file relative to the current directory, I suppose writing a bit of code 
that figures out where this package is located and modifying it won't be 
too hard.

And, at the risk of being flamed or sounding like a troll, this seems 
like something that should be easy to do...other languages manage it 
quite neatly. Up until this point I was really liking my exposure to 
Python :-/

I wonder if there is any more magic that I'm missing, the thing is the 
pkgutil method looks exactly like what I want, except for not executing 
the additional __init__.py files in the added directories.

Thanks for the help so far Peter, if anyone has a prettier solution then 
I'm all ears.

Cheers,

   n








More information about the Python-list mailing list