[New-bugs-announce] [issue43037] Importlib reload by module name (String)

Stefan Mosoi report at bugs.python.org
Wed Jan 27 07:14:36 EST 2021

New submission from Stefan Mosoi <st3f4n2006 at gmail.com>:

Weird behaviour (maybe it's my opinion) in reload from importlib

if i do:
import importlib
import sys
import datetime

I get
Typeerror: reload() argument must be a module

but if i do

import importlib
import sys
import datetime

it works.

The sys.modules.get i got from reload() code:
def reload(module)
    if not module or not isinstance(module, types.ModuleType):
        raise TypeError("reload() argument must be a module")
        name = module.__spec__.name
    except AttributeError:
        name = module.__name__

    if sys.modules.get(name) is not module:

It wouldn't be easier to check if string do sys.modules.get ?

And as a bonus a reload_module function that gets a class (or anything with __module__ and reloads that module, no question asked?) 

And i know it's easy to implement, but it would be nicer if was better handled in the lib :)

Notes (if it matters):
Python 3.9.1 x64 Windows
I didn't test on other versions.

components: Library (Lib)
messages: 385765
nosy: st3f4n2006
priority: normal
severity: normal
status: open
title: Importlib reload by module name (String)
type: enhancement
versions: Python 3.9

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list