Let's say I have a main.py and a subfile.py.
- main.py
import subfile #Writen by myself. I want to share argparse with it
import argparse
parser = argparse.ArgumentParser(description='test',formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--a', type=int, default=0, help='test')
args = parser.parse_args()
args.a=0
subfile.fun()
args.a=1
subfile.fun()
args.a=0
subfile.fun()
2.subfile.py
xxxxxsome operation here to use args in main.pyxxxx
# In tensorflow wrapped argparse, I just need to define the same ArgumentParser to use args defined in main.py.
# But it's not suitable for "argparse", which would cause conflicts.
def fun():
if args.a==0:
print('yes')
else:
print('no')
In main.py I use argparse to pass arguments, and main.py use subfile.py.
Now, I want to use args define in main.py, how can I do?
(The codes are an simplified example described the problem, which can't be solved by reorganized code and has to be solved by using args in subfile.py. )
Thanks for your help! I have been torture by some bugs for the whole day and your answer would help me out. Appreciated!
edit: Actually I know how to pass a paramter to a funciton. But as I said above, I writed the example code just for the key solution: is how to use the library "argparse" across python file, which is supported in a similar library in tensorflow "FLAG".