Suppose I need to run a python file, x.py, from within y.py, passing variable z from y.py to x.py.
How would I accomplish this? Would something like this work?
call(["python","x.py",z])
You need to encapsulate your code correctly. You should have something like this:
y.py
def y_func(*args):
# everything y does
if __name__ == "__main__":
import sys
y_func(*sys.argv)
x.py
from y import y_func
def x_func(*args):
#do stuff
y_result = y_func(*yargs)
#finish x stuff
if __name__ == "__main__":
import sys
x_func(*sys.argv)
Using subprocess is better than os.system
import subprocess
subprocess.check_output(["ls", "-ltr"])
In your case let's say you have a.py:
import sys
print sys.argv[1:]
and in the prompt:
>>> import subprocess
>>> subprocess.check_output(["python", 'a.py', 'aaa bbb ccc'])
"['aaa bbb ccc']\n"
>>>
x.pyaccept command line arguments?yin a function, thenimportthat function in toxand call it appropriately.