I have a module which accepts arguments from the command line, let's call that module A.
I would like to execute A from another module - B, so that B will set A's arguments-
without executing A as a separate process.
Any way to do that?
That depends on how module A takes it's arguments. If it only looks at sys.argv and has no __main__ test, then you could alter sys.argv:
sys.argv[1:] = ['arg1', 'arg2']
then import module A.
However, if you are writing A yourself, use a function, called from a __main__ test instead:
def main(*args):
# main code for module
if __name__ == '__main__`:
import sys
main(*sys.argv[1:])
then you can import A and call main() directly:
import A
A.main('arg1', 'arg2')
Note that if module A is not your own, and it does have a __main__ test, but does all the work in that if __name__ == '__main__': suite, then you are sunk and have to reproduce that code.
main(argv)function). I presume you can't change it to do that?