classification
Title: Import module doesn't updated
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: prasechen, steven.daprano, terry.reedy
Priority: normal Keywords:

Created on 2020-09-12 03:00 by prasechen, last changed 2020-09-18 23:09 by terry.reedy. This issue is now closed.

Messages (5)
msg376766 - (view) Author: chen-y0y0 (prasechen) Date: 2020-09-12 03:00
# I create a module:
# \sys.path\xxx.py
a = 9
# And:
>>> import xxx
>>> xxx.a
9
# I delete this imported module and modified this module:
del xxx
# \sys.path\xxx.py
a = 9
b = 8
# And re-import:
>>> import xxx
>>> xxx.b
# But:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'xxx' has no attribute 'b'
msg376767 - (view) Author: pmp-p (pmpp) * Date: 2020-09-12 03:06
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
msg376770 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-09-12 04:14
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:

    del sys.modules['modulename']
    import modulename  # reloads from the module file


or possibly simpler, use reload:

    importlib.reload(modulename)
msg376818 - (view) Author: chen-y0y0 (prasechen) Date: 2020-09-13 02:39
--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'
msg377142 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-09-18 23:09
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.
History
Date User Action Args
2020-09-18 23:09:22terry.reedysetstatus: open -> closed

nosy: + terry.reedy, - paul.moore, tim.golden, zach.ware, steve.dower
messages: + msg377142

resolution: not a bug
2020-09-13 02:39:57prasechensetstatus: closed -> open
resolution: not a bug -> (no value)
messages: + msg376818
2020-09-12 04:14:28steven.dapranosetstatus: open -> closed

type: resource usage -> behavior
components: + Interpreter Core, - Windows

nosy: + steven.daprano
messages: + msg376770
resolution: not a bug
stage: resolved
2020-09-12 03:10:58pmppsetnosy: - pmpp
2020-09-12 03:06:23pmppsetnosy: + pmpp
messages: + msg376767
2020-09-12 03:00:14prasechencreate