Execute a file with arguments in Python shell
PythonShellPython Problem Overview
I would like to run a command in Python Shell to execute a file with an argument.
For example: execfile("abc.py")
but how to add 2 arguments?
Python Solutions
Solution 1 - Python
try this:
import sys
sys.argv = ['arg1', 'arg2']
execfile('abc.py')
Note that when abc.py
finishes, control will be returned to the calling program. Note too that abc.py
can call quit()
if indeed finished.
Solution 2 - Python
Actually, wouldn't we want to do this?
import sys
sys.argv = ['abc.py','arg1', 'arg2']
execfile('abc.py')
Solution 3 - Python
execfile
runs a Python file, but by loading it, not as a script. You can only pass in variable bindings, not arguments.
If you want to run a program from within Python, use subprocess.call
. E.g.
import subprocess
subprocess.call(['./abc.py', arg1, arg2])
Solution 4 - Python
import sys
import subprocess
subprocess.call([sys.executable, 'abc.py', 'argument1', 'argument2'])
Solution 5 - Python
For more interesting scenarios, you could also look at the runpy
module. Since python 2.7, it has the run_path
function. E.g:
import runpy
import sys
# argv[0] will be replaced by runpy
# You could also skip this if you get sys.argv populated
# via other means
sys.argv = ['', 'arg1' 'arg2']
runpy.run_path('./abc.py', run_name='__main__')
Solution 6 - Python
You're confusing loading a module into the current interpreter process and calling a Python script externally.
The former can be done by import
ing the file you're interested in. execfile is similar to importing but it simply evaluates the file rather than creates a module out of it. Similar to "sourcing" in a shell script.
The latter can be done using the subprocess module. You spawn off another instance of the interpreter and pass whatever parameters you want to that. This is similar to shelling out in a shell script using backticks.
Solution 7 - Python
You can't pass command line arguments with execfile()
. Look at http://docs.python.org/library/subprocess.html">`subprocess`</a> instead.
Solution 8 - Python
If you set PYTHONINSPECT
in the python file you want to execute
[repl.py]
import os
import sys
from time import time
os.environ['PYTHONINSPECT'] = 'True'
t=time()
argv=sys.argv[1:len(sys.argv)]
there is no need to use execfile
, and you can directly run the file with arguments as usual in the shell:
python repl.py one two 3
>>> t
1513989378.880822
>>> argv
['one', 'two', '3']
Solution 9 - Python
Besides subprocess.call
, you can also use subprocess.Popen
. Like the following
subprocess.Popen(['./script', arg1, arg2])
Solution 10 - Python
If you want to run the scripts in parallel and give them different arguments you can do like below.
import os
os.system("python script.py arg1 arg2 & python script.py arg11 arg22")
Solution 11 - Python
This works:
subprocess.call("python abc.py arg1 arg2", shell=True)
Solution 12 - Python
runfile('abc.py', ['arg1', 'arg2'])
Solution 13 - Python
This works for me :
import subprocess
subprocess.call(['python.exe', './abc.py', arg1, arg2])