I have searched SO but couldn't find an asnwer. I want to invoke a python script(child script) from another python script(main script). I cannot pass arguments from parent to child? I am expecting "subprocess launched: id1-id2" from the console. But what I am getting is "subprocess launched:test-default". The subprocess is using the default parameters instead of receiving parameters from the parent script.
# parent
import subprocess
subprocess.call(['python', 'child.py', 'id1', 'id2'])
# script name: child.py
def child(id, id2):
print ('subprocess launched: {}-{}'.format(str(id), id2))
if __name__ == '__main__':
main(id='test', id2='default')
main(..)instead ofchild(..).sys.args.sys.argsbut yourchild.pydoesn't usesys.argvfork()a copy of your current interpreter with startup-time initialization already done; when you usesubprocessto start a whole new interpreter, it pays what would otherwise be one-time startup costs a second time. See the multiprocessing module for an example. And of course, there's also threading (though whether that's appropriate depends on implementation details).