Reputation: 805
I have a script that computes some stuff. It uses inputs from a separate file 'inputs.py'.
In 'inputs.py' are only a few variables:
A = 2.3
B = 4.5
C = 3.0
In the main file I import them with
from inputs import *
If I now change something in 'inputs.py' and execute the script again it still uses the old values instead of the new ones. How can I reload the file?
reload(inputs)
does not work.
Many thanks in advance!
Upvotes: 24
Views: 37186
Reputation: 23261
Let's quote docs:
reload(module)
Reload a previously imported module. The argument must be a module object, so it must have been successfully imported before. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter. The return value is the module object (the same as the module argument).
The argument must be a module object, so it must have been successfully imported before. When you do from inputs import *
you actually has no module object in your namespace. Only module members.
When reload(module) is executed:
- Python modules’ code is recompiled and the module-level code reexecuted, defining a new set of objects which are bound to names in the module’s dictionary. The init function of extension modules is not called a second time.
- As with all other objects in Python the old objects are only reclaimed after their reference counts drop to zero.
- The names in the module namespace are updated to point to any new or changed objects.
- Other references to the old objects (such as names external to the module) are not rebound to refer to the new objects and must be updated in each namespace where they occur if that is desired.
Other references to the old objects (such as names external to the module) are not rebound to refer to the new objects and must be updated in each namespace where they occur if that is desired. You star-imported A, B and C are precisely other references.
To sum up, an example code would be:
import os # see below
# fake module before changes
with open('inputs.py', 'w') as f:
f.write("a, b, c = 1, 2, 3")
import inputs
# check if all members are correct
assert inputs.a == 1
assert inputs.b == 2
assert inputs.c == 3
os.unlink('inputs.pyc') # Remove previously compiled byte-code.
# I'm now sure if it's mandatory, anyway for some reason Python
# does not recompile inputs.py in my experiments.
# New fake file
with open('inputs.py', 'w') as f:
f.write("a, b, c = 4, 5, 6")
reload(inputs)
# check if members has changes
assert inputs.a == 4
assert inputs.b == 5
assert inputs.c == 6
Upvotes: 7
Reputation: 91159
from inputs import *
does something like this pseudo code:
import inputs as temporary
for all names in temporary:
(name) = temporary.name
del temporary
The module inputs
is cached in sys.modules
.
If you do a reload(inputs)
, the cached module is reloaded, but the assignment process which loads the data from the imported module to the local name space is not repeated. You have to do so by hand, as the other answer already states.
Upvotes: 0
Reputation: 91007
If you are using Python 3.x , then to reload the names that have been imported using from module import name
, you would need to do -
import importlib
import inputs #import the module here, so that it can be reloaded.
importlib.reload(inputs)
from inputs import A # or whatever name you want.
For Python 2.x , you can simply do -
import inputs #import the module here, so that it can be reloaded.
reload(inputs)
from inputs import A # or whatever name you want.
Upvotes: 39