dynamic import with heritage

marco manatlan at online.fr
Fri Jun 11 18:38:00 EDT 2004


Grégoire Dooms a écrit :

> marco wrote:
> 
>> Grégoire Dooms a écrit :
>>
>>> marco wrote:
>>>
>>>> Grégoire Dooms a écrit :
>>>>
>>>>> In your dhuile package, you need bidon in your namespace.
>>>>> This can be done by importing the module containing bidon's 
>>>>> definition.
>>>>> According to what you say ("here my main"), this module is __main__ .
>>>>>
>>>>> So a simple
>>>>> from __main__ import bidon
>>>>> class vidange(bidon):
>>>>>    pass
>>>>> should do the job.
>>>>>
>>>>
>>>> your statement doesn't work
>>>> but it works, if i do :
>>>> -------------------------
>>>> import sys
>>>> sys.path.append("../..")
>>>> from main import bidon
>>>
>>>
>>>
>>>
>>>> -------------------------
>>>>
>>>> does it exists a way to do it easier ? (i don't like this technick 
>>>> to append a path to sys.path)
>>>>
>>>
>>> I wrote
>>> from __main__ import bidon
>>> Not
>>> from main import bidon
>>
>>
>>
>> if i wrote :
>> "from __main__ import bidon"
>>
>> i've go an "ImportError: Cannot re-init internal module __main__"
>> ?!?
> 
> 
> I think this should only happen if '__main__' is not in sys.modules.
> Could you check that '__main__' is indeed missing in sys.modules ?
> 
> import sys
> if '__main__' in sys.modules:
>     from __main__ import bidon
> else:
>     print 'Something strange happened here'
> 
> Interresting...

no ;-(
it seems that "__main__" is in sys.modules
but when it try the "from __main__ import bidon"
it makes a : "ImportError: Cannot re-init internal module __main__"

where can i find doc about this technick ?


> -- 
> Grégoire Dooms



More information about the Python-list mailing list