[New-bugs-announce] [issue40427] importlib of module results in different id than when imported with import keyword

John Andersen report at bugs.python.org
Tue Apr 28 15:00:54 EDT 2020


New submission from John Andersen <johnandersenpdx at gmail.com>:

When importing a file using importlib the id() of the object being imported is not the same as when imported using the `import` keyword. I feel like this is a bug. As if I have a package which is using relative imports, and then I import all of the files in that package via importlib. issubclass and isinstance and others no longer work when the relative imported object and then importlib imported object are checked against each other, I assume because the id()s are different.

$ cat > target.py <<'EOF'
class Target:
    pass

EOF

$ cat > importer.py <<'EOF'
import importlib
from target import Target

spec = importlib.util.spec_from_file_location("target", "target.py")
imported_by_importlib = importlib.util.module_from_spec(spec)
spec.loader.exec_module(imported_by_importlib)

print("isinstance(imported_by_importlib.Target(), Target:",
isinstance(imported_by_importlib.Target(), Target))
print("id(Target):", id(Target))
print("id(imported_by_importlib.Target):", id(imported_by_importlib.Target))

EOF

$ python importer.py
isinstance(imported_by_importlib.Target(), Target: False
id(Target): 93824998820112
id(imported_by_importlib.Target): 93824998821056

----------
messages: 367554
nosy: pdxjohnny
priority: normal
severity: normal
status: open
title: importlib of module results in different id than when imported with import keyword
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40427>
_______________________________________


More information about the New-bugs-announce mailing list