运行Python脚本的几种方法
1. 命令行运行
只需在Python脚本的尾部添加代码:
if __name__ == 'main':
test()
则该脚本可以在命令行直接被执行。
2. 在其它脚本中调用运行
对于包含了abc()函数的Python脚本test.py,在其它脚本中调用它的方法有两种:
2.1 通过import
导入
import test
test.abc(xxx)
或者
from test import abc
abc(xxx)
2.2 使用os.system()
来执行
os.system('xxx.py')
当然,除了os模块的os.system
、os.popen
,还有subprocess
模块,可以参考:Python—执行系统命令的四种方法(os.system、os.popen、commands、subprocess)