I have two files, one which has a side effect I care about that occurs within the if __name__ == "__main__"
guard:
# a.py
d = {}
if __name__ == "__main__":d['arg'] = 'hello'
The second file imports the first (using runpy
) and prints the dictionary:
# b.py
import runpy
m = runpy.run_module('a', run_name='__main__')
print(m['d']) # {'arg': 'hello'}
So far this works. But now I want to change the first file to accept a command line argument:
import sys
d = {}
if __name__ == "__main__":d['arg'] = process(sys.argv[1])
The problem is that process()
is written by someone else and outside of my control, but I still want to get the updated dictionary d
after it has been "processed".
How can I mock sys.argv
before calling runpy
, or otherwise provide that value to a.py
?