-1

I have a Python script a.py and b.py, I'd like to call the main function of b.py and pass arguments from a.py. How to add option in a.py from inside the code and pass it to the main of b.py ?. I tried adding the dictionary my additional option but for some unknown reason the b.py doesn't get the right value. The add_option only works for command line option.

I've imported b inside a.py, I'm trying to pass readOnly value to main in b.py. Basically I don't want to pass readOnly from a.py as command line but pass the readOnly through the code inside a.py.

a.py

import b

def main()
    usage = "usage: %prog [options]"
    parser = OptionParser(usage)
    (options, _ ) = parser.parse_args()
    varsOptions = vars(options)
    varsOptions['readOnly'] = True
    b.main(varsOptions)


b.py

def main(argv):
    usage = "usage: %prog [options]"
    parser = OptionParser(usage)
    parser.add_option("-r", action="store_true", dest="readOnly")
    (options, _ ) = parser.parse_args()
    varsOptions = vars(options)
    print(varsOptions)

if __name__ == "__main__":
    main(sys.argv[1:])

Why this code doesn't work ?.

TIA,

John

6
  • What have you tried so far? Is b to be imported, or run as a separate process? Commented May 25, 2012 at 18:44
  • possible duplicate of how to execute a python script file with an argument from inside another python script file Commented May 25, 2012 at 18:47
  • It's fine if you want to close it but it doesn't answer my question why my code snippet doesn't work. Commented May 25, 2012 at 19:10
  • What does b.main look like? Commented May 25, 2012 at 19:32
  • Update the question with b.py snippet. The print of varsOptions doesn't show the value True from readOnly. Commented May 25, 2012 at 19:40

3 Answers 3

2

You should see examples from official doc : http://docs.python.org/dev/library/argparse.html for argparse

For an older python, see http://docs.python.org/dev/library/optparse.html

Sign up to request clarification or add additional context in comments.

3 Comments

@YuvalAdam: No, argparse also works fine in Python 2.7, see docs.python.org/library/argparse.html.
argparse is available for many versions of python: pypi.python.org/pypi/argparse
@sputnick: It is not clear to me that the OP wants to run b.py in a separate process, perhaps import will be enough..
1

It does not work because b.main does not use the argv parameter.

You could pass argv to parse_args as a parameter, but it should be a list of strings, not a dictionary.

Try this in a:

b.main(["-r"])

Comments

0

this is not really a good idea.

but if you need to do it you can do it with the os run commands

os.system('script.py "options" ')

you should read the following stack overflow

how to execute a python script file with an argument from inside another python script file

3 Comments

Ah, thanks for the question pointer, voting to close this one as a dupe of that one!
Not sure is it because Python can't do this easily like my code excerpt ?.
This is not a dup....look at the comment from the original poster on that thread, he hasn't been able to make it to work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.