trouble with reload

Steven D'Aprano steve at
Fri Aug 14 10:34:52 CEST 2009

On Fri, 14 Aug 2009 13:49:19 +0900, Terry Reedy wrote:

> Dr. Phillip M. Feldman wrote:
>> According to the Python documentation, 'reload' reloads a previously
>> imported module (so that changes made via an external editor will be
>> effective). But, when I try to use this command, I get the following
>> error message:
>> TypeError: reload() argument must be module
>> Any suggestions will be appreciated.
> Besides the other answers, do not use reload. It is removed in Py3
> because it cannot be made to work as people reasonably expect.

That's a damn shame, because it is very useful for interactive use once 
you get it's quirks. Is it gone-gone or just removed from built-ins? 

If the former, would the following be a reasonable replacement?

def reload(module):
    if type(module) is not type(__builtins__):
        raise TypeError("reload() argument must be module")
    name = module.__name__
    del globals()[name]
    del sys.modules[name]
    globals()[name] = __import__(name)

It seems to work for me, but I'm not sure if I've missed something.


More information about the Python-list mailing list