Actually, wouldn't we want to do this?
import sys
sys.argv = ['abc.py','arg1', 'arg2']
execfile('abc.py')
Answer from user2757262 on Stack OverflowActually, wouldn't we want to do this?
import sys
sys.argv = ['abc.py','arg1', 'arg2']
execfile('abc.py')
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.
Functions variable binding and `exec`
Using Python to interact with command line program
How to set a variable as global with python’s exec() function?
Be careful with exec and eval in Python
Seems like fairly obvious but good advice to me. Didn't know about using exec in a custom namespace - that was cool. Learnt a couple other things too.
Cheers for posting!
More on reddit.comVideos
to pass arguments to exec() you need to pass dict as an argument for globals/locals not set.
exec(open('external_script.py'.read(),{'argv':'install'})
and it will create argv reference inside exec() scope, if you pass array to your external_script.py you will have to do add the following to the code
for i in argv: sys.argv.append(i)
note: if you use from sys import argv to import argv it will overwrite the value you passed with ['external_script.py']
os.system is used to execute a cmd command
import os
cmd=input("type a cmd statement here")
try:
exec(os.system(cmd))
except Exception as e:
print(e)
For you it will be
os.system("external_script.py install")
Where install is the argument to pass to external_script.py