Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

argparse - importing a python script from another script and running it with arguments

I have a python script which has been packaged up as a command line script (dbtoyaml.py in Pyrseas since you ask).

I am running another python script from which I want to call this script. Is there no way to import the module and artificially populate the required arguments from my second script to avoid changing any of the pyrseas code at all?

from pyrseas import dbtoyaml -- My initial script, which also takes arguments dbtoyaml.main(['-m','-H MYHOSTNAME' .... other options])

Hasn't yet worked for me.

I get a strange error:

usage: checkSchemaChanges.py [-h] [-H HOST] [-p PORT] [-U USERNAME] [-W]
                         [-c CONFIG] [-r REPOSITORY] [-o OUTPUT]
                         [--version] [-m] [-O] [-x] [-n SCHEMA]
                         [-N SCHEMA] [-t TABLE] [-T TABLE]
                         dbname
checkSchemaChanges.py: error: unrecognized arguments: MYHOSTNAME mydatabaseuser

Which is a mixture of my new script (checkSchemaChanges.py, and MYHOSTNAME and mydatabaseuser at the bottom) and the parameters from dbtoyaml, which are all correct.

Could it be the double set of parameters which is confusing argparse?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When I'm writing a command line script I oftentimes will specifically design my script so this is possible. The key is to parse the args separate from the main function.

For example the main function might look like this:

def main(**kwargs):
    # the body of the script goes here

Then elsewhere in the module I will configure the arg parser, parse the args and pass the result into the main script:

def run():
    parser = ... # configure parser here
    configs = parse_args(parser)
    main(**configs)

That way, if someone wants to call the script from within Python, they can do so (it also makes testing much easier):

import somescript
somescript.main(option='value', option2='value2')

Unfortunately, it appears that the authors of the script you are using did not do anything like that. As stated in another answer you can overwrite sys.argv, then import the script. While that may feel hacky, it should be less resource intensive than opening a new process and calling the command separately.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...