Python-运行外部脚本 [英] Python - run external script

查看:66
本文介绍了Python-运行外部脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道我是否有一个脚本one.py,该脚本的编写方式如下:

Does someone know if I have a script one.py which is written the following way:

if __name__ == '__main__':
    # Do something

我想从另一个脚本中调用该main函数.我该怎么办?

And I want to call that main function from another script. How should I do that?

我想可能是(假设这是launcher.py)

I guess it would be something like (let's say this is launcher.py)

# 'one' stands for import from `one.py` module
import one

if __name__ == '__main__':
    one.main()

唯一的问题是我不能用这种方式呼叫main().

应如何完成?

推荐答案

with file('a.py','rU') as f:
  co=compile(f.read(),'foobar','exec')
  exec co in {'__name__':'__main__'}

这篇关于Python-运行外部脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆