简体   繁体   English

如何在另一个python脚本的循环内调用和运行python mainmodel.py?

[英]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). 我一直很想运行一个python mainmodel.py模型(该模型使用“ mainmodel.py”模型,从命令的所有方面来看)。

The problem is I can't run this model inside the loop in python. 问题是我无法在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). 在模型的敏感性分析中,意识形态是在不同条件之间运行模型的(对于15个参数中的每一个都有很多变化)。 I treid to use this: How to run a Python script from another Python script in the cross-platform way? 我很想使用此方法: 如何以跨平台方式从另一个Python脚本运行一个Python脚本?

or 要么

How to run a python script from another python script and get the returned status code? 如何从另一个python脚本运行python脚本并获取返回的状态代码?

My code to call a "mainmodel.py" is simple, only when a run the model, my code to call the "mainmodel.py" is: 我调用“ mainmodel.py”的代码很简单,只有在运行模型时,我调用“ mainmodel.py”的代码是:

###################################################
__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!! 因为这样做(对analisys敏感)是自动化的! 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. 另一种方法是触发__main__哨兵。 For more info, have a look at: 有关更多信息,请查看:

In Python, can I call the main() of an imported module? 在Python中,我可以调用导入模块的main()吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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