I have several scripts that I wish to run consecutively (each saves their output as a file, so I don't need to look at them). I wish to create a separate python file that can run through them, executing each file once it is done with the previous. However, I am unsure how to execute one script from another.
I have created a sample subfile (script) to be run by another master file:
import numpy as np
x = np.arange(0,9).reshape((3,3))
x+=1
y=x**2
print(y)
This creates a couple matrices and does something with them. I include the print command as a test for whether it has run (perhaps this is not a good test?). The master file is below:
import os
os.system('python subfile_test.py')
# exec('sub_file')
I have tried using both the os.system() and exec() commands. When I use the os.system() command, I get no errors but I do not see the print, nor are the objects (x and y) accessible after running. When I comment out the first two lines and uncomment the exec() command, I get an error NameError: name 'sub_file' is not defined
. I get the same error when I write 'sub_file.py' instead (adding the .py to the filename). Both files (master and sub) are located in the same folder, and this is the working directory (I am using Spyder).
I have referenced How can I make one python file run another? and have tried the above methods. The preferred method (import) does not seem correct here, as I have no function/class I am importing - I just want to run a script.
What am I misunderstanding here, and can someone clarify under what situations each of these methods (import, os.system(), and exec()) are useful/not?