dynamic import with heritage

marco manatlan at online.fr
Fri Jun 11 11:36:27 EDT 2004


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__"
?!?

> __main__ is the namespace of the script you run.
> It is always already loaded so you don't need any modification to sys.path.

yes, i understand  ... but it seems it doesn't work well

> I would define bidon in base_classes.py, have the script import that 
> module and have the plugin import __main__ and refer to the bidon it via 
> from __main__.base_classes import bidon
> 
yes, it's better
but i'd like to do it like up

> Otherwise have the script add the directory of base_classes.py to 
> sys.path and the plugin just
> from base_classes import bidon
> 
> -- 
> Grégoire Dooms



More information about the Python-list mailing list