New submission from Owen Pembery :
This may be a bug, or may be a hole in my understanding of how importlib.reload
works.
In short, when running importlib.reload in a script, it seems that a module is
not reloaded before it is used, whereas running the same script a line at a
time in interactive mode seems to reload the script.
Relevant scripts are in the github repository:
https://github.com/orpembery/python-reload-bug.
I find the bug using python 3.5.2 on Ubuntu 16.04.
The expected output from running
python3 master.py
is:
1
2
However, I instead find that the output is:
1
1
If I paste the commands in master.py into the interpreter one at a time, I get
the expected behaviour:
Python 3.5.2 (default, Nov 12 2018, 13:43:14)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from shutil import copy
>>>
>>> from importlib import reload
>>> # Move a file
... copy('fn_1.py','fn.py')
'fn.py'
>>> # Import it
... import fn
>>> # Run it
... fn.fn()
1
>>> # Move a different file to the same location.
... copy('fn_2.py','fn.py')
'fn.py'
>>> # Reload it
... reload(fn)
>>> # Run it
... fn.fn()
2
However, if the commands in master.py are pasted in all at one (i.e., with a
single CTRL-V keystroke), I obtain the unexpected behaviour:
Python 3.5.2 (default, Nov 12 2018, 13:43:14)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from shutil import copy
>>> from importlib import reload
>>>
>>> # Move a file
... copy('fn_1.py','fn.py')
'fn.py'
>>>
>>> # Import it
... import fn
>>>
>>> # Run it
... fn.fn()
1
>>>
>>> # Move a different file to the same location.
... copy('fn_2.py','fn.py')
'fn.py'
>>>
>>> # Reload it
... reload(fn)
>>>
>>> # Run it
... fn.fn()
1
Possible causes:
I'm very far from an expert, but it seems to be something to do with caching;
running
python3 -B master.py
gives the expected behaviour.
--
components: Library (Lib)
messages: 330631
nosy: brett.cannon, orpembery
priority: normal
severity: normal
status: open
title: Importlib.reload has different behaviour in script and interactive mode
versions: Python 3.5
___
Python tracker
<https://bugs.python.org/issue35343>
___
___
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com