I am in the process of coding an app which has to get the metadata (author,version..etc) out of several modules (.py files) and display them. The user selects a script and the script is executed. (New scripts can be added and old ones be taken of from the target folder just like a plugin system).
Firstly I import a script and I take out the metadata, then I go for the next.
But I want to de-import all the other modules except for the one that the user has selected.
How can I implement this?
I tried these
1. del module
2. del sys.modules['module']
The latter did not work. I tried #python and got the answer that it was not good to de-import modules, but I want to know a clean way of implementing this. Any ideas/suggestions would help.
I think this post should help you.
To secure the availability of this information (in case the link dies or something similar), below is the original message from the tutor mailing list:
On 8/14/06, Dick Moores wrote:
Hi Dick,
Usually this entails removing from the "module registry and removing
references to it in the code. If you have a really well used module (like
one with config parameters imported into every module), then you’ll have an
additional step of removing it from every module. Also, if you have use
"from psyco import …", then you will not be able to free up the module and
the reference to the module easily (is it from that module, or imported from
a third module? see "if paranoid: code below).
The function below deletes a module by name from the Python interpreter, the
"paranoid" parameter is a list of variable names to remove from every other
module (supposedly being deleted with the module). Be VERY careful with the
paranoid param; it could cause problems for your interpreter if your
functions and classes are named the same in different modules. One common
occurrance of this is "error" for exceptions. A lot of libraries have one
"catch-all" exception called "error" in the module. If you also named your
exception "error" and decided to include that in the paranoid list… there
go a lot of other exception objects.
Then you should be able to use this like:
or
-Arcege