Reputation: 13
So, let's say that I got a shell script that generates a v.py on the output that contains
v = some-float
And I'm accessing it in the main.py script via from v import v
But later the main Script toggles generation process again and the variable in v.py gets updated, but the variable v
is not updating for the main script.
To make main script work I need to update the variable from v.py while script is running
I've tried importlib.reload(v)
- didn't work
I'm still new to python and don't understand it completely
Upvotes: 1
Views: 531
Reputation: 496
Presuming you are using python 3.*, v needs to be a module, so the way you import it changes to example below.
import v
import importlib
print(v.v)
with open('v.py', 'w') as f:
f.write('v = 20.0')
importlib.reload(v)
print(v.v)
Also note the document of importlib
"When a module is reloaded, its dictionary (containing the module’s global variables) is retained. Redefinitions of names will override the old definitions, so this is generally not a problem."
Though our minimal example works, this is something to bear in mind for more complicated cases.
Upvotes: 1