简体   繁体   English

从另一个python脚本调用python main

[英]Call a python main from another python script

I have been giving some huge command line tool from a colleague. 我一直在给同事提供一些巨大的命令行工具。 The main reads a bunch of arguments, parses those using the elegant import OptionParser later on and does the job. main读取了一堆参数,稍后使用优雅的import OptionParser解析那些参数并完成工作。

if __name__ == '__main__':
    main(sys.argv)

I can either dig into the code and copy paste loads of code, or find a way to use a "command line" call from my python script. 我可以深入研究代码并复制粘贴代码,或者找到一种方法来使用我的python脚本中的“命令行”调用。 I guess the second option is preferrable as it prevents me from randomly extracting code. 我想第二种选择是可取的,因为它阻止我随机提取代码。 Would you agree ? 你同意吗?

You don't need to do cut and paste or launch a new Python interpreter. 您不需要剪切和粘贴或启动新的Python解释器。 You should be able to import the other script . 您应该能够import其他脚本

For example, if your colleague's script is called somescript.py you could do: 例如,如果您的同事的脚本名为somescript.py您可以执行以下操作:

import somescript
args = ['one','two']
somescript.main(args)

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

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