Fix Python – Run a Python script from another Python script, passing in arguments [duplicate]


Asked By – Gern Blanston

I want to run a Python script from another Python script. I want to pass variables like I would using the command line.

For example, I would run my first script that would iterate through a list of values (0,1,2,3) and pass those to the 2nd script 0 then 1, etc.

I found Stack Overflow question 1186789 which is a similar question, but ars’s answer calls a function, where as I want to run the whole script, not just a function, and balpha’s answer calls the script but with no arguments. I changed this to something like the below as a test:

execfile(" 1")

But it is not accepting variables properly. When I print out the sys.argv in it is the original command call to first script “[‘C:\’].

I don’t really want to change the original script (i.e. in my example) since I don’t own it.

I figure there must be a way to do this; I am just confused how you do it.

Now we will see solution for issue: Run a Python script from another Python script, passing in arguments [duplicate]


Try using os.system:

os.system(" 1")

execfile is different because it is designed to run a sequence of Python statements in the current execution context. That’s why sys.argv didn’t change for you.

This question is answered By – Greg Hewgill

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0