Forum Archive

Run python script with args

C0deH4cker

Is it possible to run one Python script from another with a given value for sys.argv? I looked at various functions such as execfile(), but none that I saw supported modification of sys.argv.

omz

You could just assign to sys.argv before calling execfile, e.g.:

import sys
import os
script_name = 'script2.py'
script_path = os.path.join(os.getcwd(), script_name)
prev_argv = sys.argv
sys.argv = [script_path, 'foo', 'bar']
execfile(script_path)
sys.argv = prev_argv
C0deH4cker

Okay I feel stupid now for not trying that lol. Thank you.