importing dynamic files, unimporting

Olivier Dagenais olivierS.dagenaisP at canadaA.comM
Tue Oct 17 23:19:01 EDT 2000


I asked a similar question a while back, since I want to load code from a
database and the database may be updated, so I then need to update the code,
too.

The answer is not to use import, but exec() (or execfile()), although
"importing over" also works (it just isn't as flexible).  It will have about
the same effect as "from modulename import *", except that you don't need to
have a file/module on disk.

To test this out and convince myself it would work, I created a few scripts,
attached to this post.  The one to try running is dynamic.py.  It starts by
importing from one of the "lessive" files. ('lessive' is french for
detergent - I just need a name to play with)  You can see the value of
"lavage" (clothes wash) corresponding to the imported 'lessive', throughout
code (re-)loading.  Then, you can see how a new class definition (Jolt) is
loaded and how it is immediately available.

Anyway, the only thing to be careful of, is that previous instances of a
redefined class (or I guess any reference to an old thing that is redefined)
will still use the old definition.

I hope this helps you..  :)

man-I-just-LOVE-Python-for-being-so-dynamic-ly y'rs

--
----------------------------------------------------------------------
Olivier A. Dagenais - Software Architect and Developer
"Someone called 'Type your name here' is impersonating me on the
internet and is posting exactly the same things I am posting!"


"Alex McHale" <daimun at home.com> wrote in message
news:jh8H5.150548$Qx4.4968771 at news1.rdc1.il.home.com...
> Hi there,
>   I was just wondering if there is a way to import a module where the
module
> name is dynamic (basically contained in a string).  The idea is that the
> module will contain specific functions (ie def foobar( arg1, arg2 ) in all
> such modules).
>   The second part is removing these imports.  Is there a way to 'unimport'
a
> module?  The python website seems to be very lacking on such topics.
>
>   Daimun
>   daimun at home.com
>
>


begin 666 dynamic.py
M9G)O;2!L97-S:79E97@@:6UP;W)T("H-"@T*9&5F(&QO861#;V1E("@@8V]D
M94E$("DZ#0H)9&5F:6YI=&EO;B ]("(B#0H):$9I;&4@/2!O<&5N("@@(G9E
M<G-I;VXE9"YP>2(@)2!C;V1E240L(")R(B I#0H)9F]R(&QI;F4@:6X@:$9I
M;&4N<F5A9&QI;F5S*"DZ#0H)"61E9FEN:71I;VX@/2!D969I;FET:6]N("L@
M;&EN90T*"61E9FEN:71I;VX@/2!D969I;FET:6]N("L@(EQN(@T*"65X96,@
M9&5F:6YI=&EO;B!I;B!G;&]B86QS*"D-"@T*<')I;G0@;&%V86=E#0H-"FQO
M861#;V1E("@@,2 I#0IM>4IO;'0@/2!*;VQT("@@*0T*;7E*;VQT+F1E<V-R
M:6)E("@@*0T*#0IP<FEN="!L879A9V4-"@T*;&]A9$-O9&4@*" R("D-"FUY
M2F]L=" ]($IO;'0@*" I#0IM>4IO;'0N9&5S8W)I8F4@*" I#0H-"G!R:6YT
)(&QA=F%G90T*
`
end

begin 666 lessive.py
5;&%V86=E(#T@(DQE<W-I=F4@(S$B
`
end

begin 666 lessiveex.py
5;&%V86=E(#T@(DQE<W-I=F4@(S(B
`
end

begin 666 version1.py
M9G)O;2!L97-S:79E(&EM<&]R=" J#0H-"F-L87-S($IO;'0Z#0H)9&5F(%]?
M:6YI=%]?("@@<V5L9B I. at T*"0ES96QF+FED(#T@(G9E<G-I;VXQ+G!Y(@T*
M#0H)9&5F(&1E<V-R:6)E("@@<V5L9B I. at T*"0EP<FEN=" B2F]L="!W87,@
M9&5F:6YE9"!I;B G)7,G+B(@)2!S96QF+FED#0H)"7!R:6YT("),97-S:79E
5(#T@)7,B("4@;&%V86=E#0H-"@T*
`
end

begin 666 version2.py
M9G)O;2!L97-S:79E97@@:6UP;W)T("H-"@T*8VQA<W, at 2F]L=#H-"@ED968@
M7U]I;FET7U\@*"!S96QF("DZ#0H)"7-E;&8N:60@/2 B=F5R<VEO;C(N<'DB
M#0H-"@ED968 at 9&5S8W)I8F4@*"!S96QF("DZ#0H)"7!R:6YT(")*;VQT('=A
M<R!D969I;F5D(&EN("<E<R<N(B E('-E;&8N:60-"@D)<')I;G0@(DQE<W-I
7=F4@/2 E<R(@)2!L879A9V4-"@T*#0H`
`
end




More information about the Python-list mailing list