unloading a module created with imp.new_module

Ned Batchelder ned at nedbatchelder.com
Mon Nov 24 12:44:10 CET 2014


On 11/23/14 5:10 AM, Patrick Stinson wrote:
> I am defining a single class with a destructor method that prints
> ‘__del__’, and running that source code string using exec with the
> module’s dict like so:
>
> import rtmidi
> importsys
> import types
> importtime
> importgc
>
> s= """
> class A:
>      def __del__(self):
>          print('__del__')
> a = A()
> """
>
> m = types.ModuleType('mine')
> exec(s, m.__dict__)
> print('deleting...')
> m= None
> print('done')
>
> and the output is:
>
> deleting...
> done
> __del__
>
> I the “__del__" to come between “deleting…” and “done”. This is not
> being run from the interactive interpreter by via a .py file.
>

Let's look at this another way: Why do you need the module to be 
unloaded?  Isn't it enough to have the new code loaded?

-- 
Ned Batchelder, http://nedbatchelder.com




More information about the Python-list mailing list