dynamic import with heritage

marco manatlan at online.fr
Fri Jun 11 10:44:30 EDT 2004


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)

> If you want your dhuile package to be more portable, it would be better 
> to define bidon in a separate module (e.g. a "contenants.py" module ) 
> and get both your main and dhuile import it.
> 
> Hope it helps.
> -- 
> Grégoire Dooms
> 
> marco wrote:
> 
>> i try to make a dynamic import of a plugin which herits from another 
>> class
>>
>> here my main:
>> -------------------------------------------------------------------
>> class bidon:
>>     pass
>>
>> plugin = __import__( "plugins.dhuile" , globals(), locals())
>> -------------------------------------------------------------------
>>
>> And in the file /plugins/dhuile/__init__.py, i've got :
>> -------------------------------------------------------------------
>> class vidange(bidon):
>>     def test():
>>         return "ok"
>> -------------------------------------------------------------------
>>
>> python2.3 gives me :
>> "__init__.py : NameError: name 'bidon' is not defined"
>>
>> i don't understand where is the mistake ?!
>> (class "bidon" is in passed by the globals() ... but it seems 
>> __init__.py doesn't understand)
>>
>> anybody have got an idea ?
>> thanx



More information about the Python-list mailing list