[Pythonmac-SIG] Reload Module from File (quick hack)

TattooMabonzoK. TattooMabonzoK.
Thu, 21 Sep 2000 15:04:15 +0200


Yo,

I hope it's not bad netiquette to post unsolicited short code snippet to =
list...

Here's a quick hack that's prove quite useful for me.

It re-parses and re-initializes a loaded module from its associated =
source file by using the builtin 'reload' fuction. Quoting the doc: =
'This is useful if you have edited the module source file using an =
external editor and want to try out the new version without leaving the =
Python interpreter'

This hack is designed to be used from PythonIDE. It refreshes the Module =
associated with the file being edited in the front window.

I'd suggest copying into a file named something like 'Reload Module =46rom=
 File' and putting that file in your PythonIDE 'Scripts' folder.

import os=0Dimport sys=0Dimport W=0Dimport Win=0D=0Dapp =3D =
W.getapplication()=0Dwid =3D Win.FrontWindow()=0Dif wid and =
app._windows.has_key(wid):=0D	f =3D app._windows[wid].path=0D	dirname =
=3D os.path.dirname(f)=0D	module =3D os.path.basename(f)[:-3]=0D	=
exec 'import ' + module =0D	module =3D eval(module)=0D	=
sys.path.insert(0, dirname)=0D	reload(module)=0D	del sys.path[0]=0D=
	print "Reloaded module '%s' from '%s'." % (module.__name__, f)=0D=


Enjoy,

=3D tmk =3D

PS: This hack accesses some private structure from the IDE. But until we =
have a clean API to the IDE it will make do :-)...=