简体   繁体   中英

How to pass argument to script with ArgumentParser when invoked from another script

I have the following script:

first_script.py :

def register_arguments(subparser):
    subparser.add_argument(
        "-c", "--config",
    )

def __main__():
    parser = argparse.ArgumentParser()
    register_arguments(parser)
    args = parser.parse_args()
    do_something(...)

if __name__ == "__main__":
    __main__()

This script is invoked with command like and possibly adding --config='value'

Now I have issue where I need to invoke this script from second_script.py and I want also to pass config value

second_script.py :

from first_script import __main__ as func

if __name__ == "__main__":
    func()

This works fine and I can execute:

python second_script.py

Now my problem is that I actually want to hard code the config. I tried to do:

if __name__ == "__main__":
    func(config='value')

or

if __name__ == "__main__":
    func('config=value')

but this is not working. How can I pass config from second_script to the first_script so it will mimic the way arguments are passed from command line?

To note: first_script.py is not mine. I'm just using it. Thus any solution involving changing it will not work.

If you have access to the first script

Firstly one should not name a function __name__ . This name is technically reserved and could brake in future python versions. Just call the function main and leave if __name__ == "__main__": as it is.

To allow the script to be used by import one should add function arguments to main , so it runs without command line arguments.

As a quick fix one could just redefine the function as

def main(*args):
    parser = argparse.ArgumentParser()
    register_arguments(parser)
    args = parser.parse_args(*args)
    do_something(...)

then

if __name__ == "__main__":
    func('--config', 'value')

should work fine (depending on the arguments allowed).

But one should better only use argparse for command line usage and use function arguments for this.

If you cannot edit script 1

Don't import but call the script from your second one.

import subprocess
process = subprocess.Popen(
              ['python', 'first_script.py', '--config', 'value'],
              stdout=subprocess.PIPE, 
              stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
stdout, stderr

stdout should now contain the results.

You can modify the command line arguments directly:

import sys
from first_script import __main__ as func

if __name__ == "__main__":
    # modify the command line, leaving the program name as is
    sys.argv[1:] = ["--config", "value"] # sys.argv will then look like this : ["second_script.py", "--config", "value"]
    func()

This works, because parser.parse_args() reads the arguments from sys.argv by default. But you should rename your __main__ function to main , and then modify the imports accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM