使用标志变量从另一个python脚本执行python求解器

发布于 2025-02-06 02:08:00 字数 818 浏览 3 评论 0原文

我目前正在使用Python Pipline工作,在该管道中,我需要从Python代码调用不同的求解器/脚本并执行它们。由于将涉及不同的代码,因此我想根据标志参数称这些求解器为其称为这些求解器,并相应地运行它们。以下是我打算做的示例:

因此,有两个或多个Python代码,通常具有100行代码,但是在这里我以通用的示例

#script1.py
import numpy
        
def test1():
    print('This is script One')


if __name__ == '__main__':
    test1()
#script2.py
import numpy
        
def test2():
    print('This is script two')


if __name__ == '__main__':
    test2()

从另一个脚本main执行了一个通用示例。 py使用

#main.py
import numpy
from script1 import *
from script2 import *
    
if __name__=='__main__':
    test1()
    test2()

main.py执行我不想要的两个。我想使用标志变量并执行我需要的任何一种。可以使用argparse命令行解析完成此操作吗?因为每个script.py在它运行中可能会有更多标志。那也是我更喜欢的东西。

谢谢

I'm currently working on a python pipline where I need to call different solvers/scripts from a python code and execute them. Since there would be different kind of codes involved, I would like to call these solvers based on flag parameters and run them accordingly. Below is an example of what I intend to do:

So there are two or more python codes, typically with 100s of lines of code, but here I have taken a generic example

#script1.py
import numpy
        
def test1():
    print('This is script One')


if __name__ == '__main__':
    test1()
#script2.py
import numpy
        
def test2():
    print('This is script two')


if __name__ == '__main__':
    test2()

At the moment I execute them from another script main.py with

#main.py
import numpy
from script1 import *
from script2 import *
    
if __name__=='__main__':
    test1()
    test2()

This main.py executes both of them which I don't want. I would like to use flag variables and execute them whichever I need to. Can this be done using argparse command line parsing ? As there might be further flags in each script.py for it to run. That would be something I prefer as well.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的往事 2025-02-13 02:08:00

您可以做类似的事情:

#main.py
import numpy
import sys
from script1 import test1
from script2 import test2
    
if __name__=='__main__':
    # sys.argv[0] is the executable so we skip it.
    for arg in sys.argv[1:]   
        if arg == "test1": 
            test1()
        elif arg == "test2": 
            test2()

现在您可以执行测试

python main.py test1

python main.py test2

# or if you want to repeat a test several times or run multiple tests

# will execute test1, then test2 then test1 again
python main.py test1 test2 test1

You can do something like:

#main.py
import numpy
import sys
from script1 import test1
from script2 import test2
    
if __name__=='__main__':
    # sys.argv[0] is the executable so we skip it.
    for arg in sys.argv[1:]   
        if arg == "test1": 
            test1()
        elif arg == "test2": 
            test2()

Now you can execute your tests as

python main.py test1

python main.py test2

# or if you want to repeat a test several times or run multiple tests

# will execute test1, then test2 then test1 again
python main.py test1 test2 test1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文