11

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?

asked Feb 10, 2015 at 19:43

2 Answers 2

17

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.

answered Feb 10, 2015 at 21:07
Sign up to request clarification or add additional context in comments.

1 Comment

I've started doing this and it has made my scripts much more flexible and reusable. Great suggestion.
9

this seems like not the best way to do it but you can probably set sys.argv

import sys
sys.argv += ['-m','-H MYHOSTNAME' .... other options]
from pyrseas import dbtoyaml
dbtoyaml.main()

but really I have no idea what dbtoyaml.py lookslike or is

answered Feb 10, 2015 at 19:47

1 Comment

That, worked, but I had to replace sys.argv, not add to it, i.e. sys.argv = ... thanks

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.