1

Let's say I have a main.py and a subfile.py.

  1. 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".

WBR
  • 57
  • 1
  • 11

2 Answers2

2

You can add another file, let's call it arguments.py for example.

Then include at the top of both main.py and subfile.py the following line:

from arguments import args

You then move all the code relating to parsing the command line arguments to arguments.py.

This way both the modules in main.py and subfile.py have access to the same args object.

Jasmijn
  • 9,370
  • 2
  • 29
  • 43
0

def


When you define a function, if you need parameters, you can add them between the parentesis.
This is the model:

def fun(par: type) -> ReturnedType:
    pass

For example:

def fun(a: int) -> None:
    if a == 0:
        print("Yes")
    else:
        print("No")

Then, when calling subfile.fun() add the argument like this:

subfile.fun(a = a)

or like this:

subfile.fun(a)

return


Remember your function doesn't return anything.
If you want to assign to a variable "yes" or "no" you can do like this:

def fun(a: int) -> str:
    if a == 0:
        return "Yes"
    else:
        return "No"

If you want to return only "yes" or "no", you might like to return a boolean instead:

def fun(a: int) -> bool:
    if a == 0:
        return True
    else:
        return False

You can also write the if statement in one line:

print('Yes') if (a == 0) else print('No)

import


To import subfile.py the file should be in the same directory. Otherwise you have to do like this:

from sys import path
# Imagine you need C:\\Python\MyFolder\subfile.py
path.append(r"C:\\Python\MyFolder")
import subfile
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
  • 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: how to use the library "argparse" across python file, which is supported in a similar library in tensorflow "FLAG". Could you help me with this "argparse" library? – WBR Sep 05 '21 at 10:01
  • @陈绍伍, why don't you simply put the definition of fun() in the same file? Now I understand, but I don't know your answer, sorry. – FLAK-ZOSO Sep 05 '21 at 10:05
  • Please don't ask others to accept your answer or to upvote it. – Wai Ha Lee Sep 05 '21 at 15:41
  • @WaiHaLee, ok, I did it just because who asked the question was new to the site, so probably them didn't know about this – FLAK-ZOSO Sep 05 '21 at 18:36
  • The user who asked this question has been a member for two years. If you must talk about upvotes or accepting an answer, don't put it in your answer. – Wai Ha Lee Sep 05 '21 at 19:15
  • 1
    Ok, sorry, I won't do it anymore – FLAK-ZOSO Sep 05 '21 at 19:16