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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With