dynamic import with heritage

M

marco

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
 
?

=?ISO-8859-1?Q?Gr=E9goire_Dooms?=

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.

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.
 
M

marco

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 :
 
?

=?ISO-8859-15?Q?Gr=E9goire_Dooms?=

marco said:
Grégoire Dooms a écrit :


your statement doesn't work
but it works, if i do :

I wrote
from __main__ import bidon
Not
from main import bidon

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

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

Otherwise have the script add the directory of base_classes.py to
sys.path and the plugin just
from base_classes import bidon
 
M

marco

Grégoire Dooms a écrit :
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
 
?

=?ISO-8859-15?Q?Gr=E9goire_Dooms?=

marco said:
Grégoire Dooms a écrit :



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...
 
P

Peter Abel

marco said:
Grégoire Dooms a écrit :

your statement doesn't work
but it works, if i do :

Try:

import plugins.dhuile as plugin

class vidange(plugin.bidon):
def test(self):
...
...
Should work

Bonne chance.
Regards
Peter
 
M

marco

Grégoire Dooms a écrit :
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 ?

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top