Reputation: 185
I need to execute a python script from within another python-script multiple times with different arguments. I know this sounds horrible but there are reasons for it. Problem is however that the callee-script does not check if it is imported or executed (if __name__ == '__main__': ...).
Upvotes: 0
Views: 260
Reputation: 3000
Found the solution for you. You can reload a module in python and you can patch the sys.argv.
Imagine echo.py is the callee script you want to call a multiple times :
#!/usr/bin/env python
# file: echo.py
import sys
print sys.argv
You can do as your caller script :
#!/usr/bin/env python
# file: test.py
import sys
sys.argv[1] = 'test1'
import echo
sys.argv[1] = 'test2'
reload(echo)
And call it for example with : python test.py place_holder
it will printout :
['test.py', 'test1']
['test.py', 'test2']
Upvotes: 2