I have a module that I need to import and modify certain values โโof variables inside an imported instance of this module, and then execute it.
Please note that I cannot make any changes to the imported module due to outdated reasons.
Here is what I am trying to do:
let's say the module I want to import, a.py, looks like
var1 = 1 var2 = 2 if __name__ == '__main__': print var1+var2
Now I am importing this into b.py calling the script. I am trying to import it, change the value of the var1 variable and run it as the main program using runpy as follows.
import runpy import a a.var1 = 2 result = runpy._run_module_as_main(a.__name__)
But this result is output only as 3 , and not as 4 , as expected.
Any other way to achieve this (other than using runpy) without changing anything in a.py? Open any third-party module, since I do not need to make changes to the imported module.
I am using python 2.6
source share