简体   繁体   中英

How I can call and run the python mainmodel.py inside a loop in another python script?

I have been treid to run a python mainmodel.py model (tha model uses a "mainmodel.py" model, from everything is commanded).

The problem is I can't run this model inside the loop in python. The ideia is run the model between diferent conditions, in sensibility analysis of the model (with many variations for each one of 15 parameters). I treid to use this: How to run a Python script from another Python script in the cross-platform way?

or

How to run a python script from another python script and get the returned status code?

My code to call a "mainmodel.py" is simple, only when a run the model, my code to call the "mainmodel.py" is:

###################################################
__author__ = 'Isaque'
import mainmodel
import subprocess
import sys

for t in range(3):
    print 'MAJOR LOOP %s T'%t
    for i in range(2):
        subprocess.call((sys.executable, "mainmodel.py"))
        print 'MINOR LOOP %s'%i

#################################################

The problem is, we need to test many parameters inside the model. Because to do that (in sensibility of analisys) automated!! Thanks in advance!! Isaque

        #Please check the below code. Hope it helps.

        #Demo for mainmodel.py
        import sys

        def check_args(temp1,temp2):
            if temp1 == temp2:
                return True
            else:
                return False

        def main():
            arg1 = sys.argv[1]
            arg2 = sys.argv[2]
            print "I am in mainmodel.py"
            ret = check_args(arg1,arg2)
            if ret:
                #print "Success"
                sys.exit(0)
            else:
                #print "Fail"
                sys.exit(1)

        if __name__ == '__main__':
            main()
    #=========================================================

    #Calling mainmodel.py with args testing 

    import os
    for t in range(3):
        print 'MAJOR LOOP %s T'%t
        for i in range(3):
            print 'MINOR LOOP %s'%i
            cmd =  "python mainmodel.py "+ str(t) +" " + str(i) 
            print cmd
            ret_main_model = os.system(cmd)
            if ret_main_model == 0:
                print "Success"
            else:
                print "Fail"
            print "-----------------------------"

#=========================================
#Output

C:\Users\Administrator\Desktop>python call.py
MAJOR LOOP 0 T
MINOR LOOP 0
python mainmodel.py 0 0
I am in mainmodel.py
Success
-----------------------------
MINOR LOOP 1
python mainmodel.py 0 1
I am in mainmodel.py
Fail
-----------------------------
MINOR LOOP 2
python mainmodel.py 0 2
I am in mainmodel.py
Fail
-----------------------------
MAJOR LOOP 1 T
MINOR LOOP 0
python mainmodel.py 1 0
I am in mainmodel.py
Fail
-----------------------------
MINOR LOOP 1
python mainmodel.py 1 1
I am in mainmodel.py
Success
-----------------------------
MINOR LOOP 2
python mainmodel.py 1 2
I am in mainmodel.py
Fail
-----------------------------
MAJOR LOOP 2 T
MINOR LOOP 0
python mainmodel.py 2 0
I am in mainmodel.py
Fail
-----------------------------
MINOR LOOP 1
python mainmodel.py 2 1
I am in mainmodel.py
Fail
-----------------------------
MINOR LOOP 2
python mainmodel.py 2 2
I am in mainmodel.py
Success
-----------------------------

C:\Users\Administrator\Desktop>

An alternative approach is to trigger the __main__ sentinel. For more info, have a look at:

In Python, can I call the main() of an imported module?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM