Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass arguments to exec() in python

I have converted my python script into exe using pyinstaller. This exe needs to call another python script. Now pyinstaller binds the python interpreter so we can call any other python script like below:

exec(open('external_script.py').read())

This will execute external_script.py. I want to perform external_script.py install using exec. So I also want to pass install as an argument. Something like below:

exec(open('external_script.py').read(), {'install'})

In above line of code, I am passing install as argument. But here I am not sure if this is correct syntax or not. Can anyone please tell me how can we pass arguments in exec python. Thanks

like image 574
S Andrew Avatar asked Oct 29 '25 01:10

S Andrew


1 Answers

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']

like image 171
OZ_ Avatar answered Oct 30 '25 22:10

OZ_