dynamic import with heritage

Grégoire Dooms dooms at info.LESS.ucl.SPAM.ac.be
Fri Jun 11 14:42:32 EDT 2004


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...
--
Grégoire Dooms



More information about the Python-list mailing list