Odd behavior with imp.reload and logging

Andrew Berg bahamutzero8825 at gmail.com
Fri Sep 23 09:57:32 EDT 2011


On 2011.09.22 03:12 AM, Chris Angelico wrote:
> In theory, this should mean that you load it fresh every time - I
> think. If not, manually deleting entries from sys.modules might help,
> either with or without the list of modules.
I've played around with sys.modules, and it seems there are issues with
logging being reloaded (not sure if it's fair to call it a bug), not my
module. The only reliable way I've found to start fresh is to delete at
least logging from sys.modules (I can even do this from within my
module). Even if the module that imports logging is deleted, things in
logging persist (I can confirm at least handlers and handles to log
files; the latter requiring garbage collection as well to free up).
However, this would probably wreak havoc on other code that uses logging
(although, I could always import logging as something else).

On a side note, this actually gives me a good way to detect that the
module is reloaded (I can't think of any other way to detect it, at
least not off the top of my head).
-- 
CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 6.0.2



More information about the Python-list mailing list