Unloading an Unused Module in Python (A Very Specific Case)
Question: First of all, I must tell you that I have already looked this this
[bug](http://bugs.python.org/issue9072) and I understand that the feature is
(in general) not possible for a long time. However, I have a use case which is
very specific. Hence, I will try to describe my use case, and ask for
suggestions.

I am writing an interactive python application which is run from the
interpreter. So, the user might make a mistake or two when importing modules,
and I would like it, if I could provide a method for the user to delete the
module (as soon as he has imported it).

So, one of the problems with the references to the module being already
incorporated into other objects is gone. Once, I am sure that the module has
not been used at all, what can I do to remove it? Is it still technically
possible?

I was thinking that if I could create a function which manually deletes every
object/function created by the module when imported, then what I want might be
accomplished. Is this true?

IPython does a [similar operation](http://ipython.org/ipython-
doc/dev/api/generated/IPython.core.extensions.html#module-
IPython.core.extensions) with its extensions. Is that the correct way to go?


Answer: Modules are just a namespace, an object, stored in the `sys.modules` mapping.

If there are no other references to anything belonging in that module, you can
remove it from the `sys.modules` mapping to delete it again:

    
    
    >>> import timeit
    >>> import sys
    >>> del timeit  # remove local reference to the timeit module
    >>> del sys.modules['timeit']  # remove module
    

