1

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

2
  • I'm not sure I understand your question. What is mainmodel.py? How does it gets its parameters? Do you really need to be calling it as a script, or could you import it and run its functions instead? Commented Aug 21, 2015 at 3:24
  • Hi Gonzalon, the "mainmodel.py" is the controller of model. From the "mainmodel" is called all parts of the model. The idea is use "control" model to call the "mainmodel" to run as 16 parameters (each one with 20 different values). Inside this code I develop a sequence to select the parameter. But I could to that in the "control", but I can't tranfer the selected value from "control" to "generator" module. Chers!! Commented Aug 22, 2015 at 11:51

2 Answers 2

3
        #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>
Sign up to request clarification or add additional context in comments.

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.