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

2020-04-29 Thread John Andersen


John Andersen  added the comment:

Thank you! :) I must have missed that somehow

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2020-04-28 Thread Brett Cannon


Brett Cannon  added the comment:

That's expected because you are constructing a completely new module object 
with importlib.util.module_from_spec(). You're also completely circumventing 
sys.modules with the code you wrote which is the only way you would get 
equivalent IDs compared to using the import statement.

So this is working as intended.

--
nosy: +brett.cannon
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2020-04-28 Thread John Andersen


New submission from John Andersen :

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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com