You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# I create a module:# \sys.path\xxx.pya=9# And:>>>importxxx>>>xxx.a9# I delete this imported module and modified this module:delxxx# \sys.path\xxx.pya=9b=8# And re-import:>>>importxxx>>>xxx.b# But:Traceback (mostrecentcalllast):
File"<stdin>", line1, in<module>AttributeError: typeobject'xxx'hasnoattribute'b'
--REOPEN--
>>> # I try:
>>> import xxx
>>> del sys.modules['xxx']
>>> # But:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
del sys.modules['xxx']
NameError: name 'sys' is not found
>>> # I try to import sys.
>>> import sys
>>> del sys.modules['xxx']
>>> import xxx
>>> xxx.b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'xxx' has no attribute 'b'
I carefully redid your test, corrected, 3 times, and it works as expected.
Win 10, 3.9.0rc2.
>>> import a.tem3 as t
>>> t.b
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
t.b
AttributeError: module 'a.tem3' has no attribute 'b'
# Add 'b = 2' to a/tem3.py and save.
>>> import sys
>>> del sys.modules['a.tem3']
>>> import a.tem3 as t
>>> t.b
2
Without seeing exactly what you did and when, I can only guess that you imported a version of xxx without the 'b' binding. I once added it but *forgot to save* before re-importing. Note that dictionary deletion and importing are well tested operations, so that bugs in normal imports are very unlikely. If you really get a failure, there must be some relevant details that you have omitted, like importing from a remote system that is locally cached, with the local cache unchanged by editing the remote original. Or having an OS bug such that saving a file does not update the 'last changed' time.
If you disagree with a closing, please request re-opening and give us a chance to reproduce and discuss first rather than doing so yourself.
Activity
cheny0y0 commentedon Sep 12, 2020
pmp-p commentedon Sep 12, 2020
Hi, you just need to call del sys.modules['xxx'] and have a look at https://docs.python.org/3.7/library/importlib.html#importlib.invalidate_caches
before importing again
stevendaprano commentedon Sep 12, 2020
This is not a bug, it is working as designed. Importing takes the module from the cache. You can either delete the module from the cache:
or possibly simpler, use reload:
2 remaining items
cheny0y0 commentedon Sep 13, 2020
terryjreedy commentedon Sep 18, 2020
I carefully redid your test, corrected, 3 times, and it works as expected.
Win 10, 3.9.0rc2.
# Add 'b = 2' to a/tem3.py and save.
Without seeing exactly what you did and when, I can only guess that you imported a version of xxx without the 'b' binding. I once added it but *forgot to save* before re-importing. Note that dictionary deletion and importing are well tested operations, so that bugs in normal imports are very unlikely. If you really get a failure, there must be some relevant details that you have omitted, like importing from a remote system that is locally cached, with the local cache unchanged by editing the remote original. Or having an OS bug such that saving a file does not update the 'last changed' time.
If you disagree with a closing, please request re-opening and give us a chance to reproduce and discuss first rather than doing so yourself.