简体   繁体   English

如何使用 `subprocess` 模块执行 __name__== '__main__ ' * without* 下面的 python 代码?

[英]How to execute the python code below __name__== '__main__ ' *without* using the `subprocess` module?

I have a python script with the form我有一个格式为 python 的脚本

...
if __name__=='__main__':
   parser = argparse.ArgumentParser()
   #code

From another python script, how can I execute the code below __name__ == '__main__' without using the subprocess module?从另一个 python 脚本中,如何在使用subprocess进程模块的情况下执行__name__ == '__main__'下面的代码?

[Edit] In addition, my python script takes input parameters with the argparse module. [编辑] 此外,我的 python 脚本使用argparse模块获取输入参数。 I also need to pass parameters when executing the code below the __name__ part.在执行__name__部分下面的代码时,我还需要传递参数。

you might be able to get away with:你也许可以逃脱:

import sys
imp_new_module = type(sys)
new_module = imp_new_module(module_name)
new_module.__dict__['__name__'] = '__main__'
exec(open(scriptname).read(), new_module.__dict__)

But you shouldn't do it... :)但你不应该这样做...... :)

(updated question) (更新的问题)

Oh, since you're at it you can also import os, sys and then update sys.argv to what you want to pass on (eg sys.argv = [os.path.abspath('file.py'), '--my', 'par']) ... did I mention that you really shouldn't?哦,既然你在这,你也可以import os, sys然后更新sys.argv到你想要传递的内容(例如sys.argv = [os.path.abspath('file.py'), '--my', 'par']) ...我有没有提到你真的不应该这样做?

Seriously, if you have control over the script that you are calling you might want to put the code under if __name__ == '__main__': in a function说真的,如果您可以控制正在调用的脚本,您可能希望将代码放在if __name__ == '__main__': function 中

def main(argv):
    ... 
    parser.parse_args(argv)
    ...

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

Or better still define a function (or functions, object) in the script that provides an API better suited to be called from a script, which is also called from __name__ == '__main__' based on what is found in sys.argv .或者更好的是在脚本中定义一个 function(或函数、对象),以提供更适合从脚本调用的 API,该脚本也根据sys.argv中的内容从__name__ == '__main__'调用。

暂无
暂无

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

相关问题 如何从另一个python文件执行`if __name__ ==“__main__”`中的代码 - How to execute code in `if __name__ == "__main__"` from another python file 如果 __name__ == '__main__' python - if __name__ == '__main__' python 没有“if __name__ =='__ main__'的python3.x多处理循环:” - python3.x multiprocessing cycling without “if __name__ == '__main__':” 如何制作一个具有并行运行命令行工具功能的python模块(不使用if __name__ =='__main__':因此可导入)? - How to make a python module with function to run command line tools in parallel (w/o using if __name__ == '__main__': so it's importable)? 运行模块时如何使 __name__ == '__main__' - How to make __name__ == '__main__' when running module 使用 if __name__ == '__main__' 时代码不运行 - code doesnt run when using if __name__ == '__main__' 使用 sphinx 记录 python 脚本条目 (__name__ == '__main__') - Documenting python script entry (__name__ == '__main__') using sphinx 在Python多处理中使用__name __ =='__ main__'的解决方法 - Workaround for using __name__=='__main__' in Python multiprocessing 使用python-multiprocessing与if __name__ =='__main__'相关的谜 - enigma using python-multiprocessing related with if __name__ == '__main__' 如何使用__name __ =='__main__'在Linux和Windows上运行python应用程序 - how to run python application on Linux and Windows with if __name__== '__main__'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM