制作一个包含三个Python代码的单一脚本
我有三个Python代码,我想让它们一个接一个自动运行,这样最终只需要一次调用就能得到结果。请问我该如何把这些代码放到一个脚本里呢?这三个代码分别是 model-multiple.py
、align2d.py
和 model-single.py
。
其中 model-multiple.py
的内容是:
from modeller import * # Load standard Modeller classes
from modeller.automodel import * # Load the automodel class
log.verbose() # request verbose output
env = environ() # create a new MODELLER environment to build this model in
env.io.atom_files_directory = ['.', '../atom_files']
a = automodel(env,
alnfile = '3NTD_align.ali', # alignment filename
knowns = ('3NTDA'),
sequence = 'target', # code of the target
assess_methods=(assess.DOPE, assess.GA341,assess.normalized_dope))
a.starting_model= 1 # index of the first model
a.ending_model = 1 # index of the last model
# (determines how many models to calculate)
a.make() # do the actual homology modeling
3 个回答
1
你可能需要考虑一下如何整理你的脚本,这样既方便从其他脚本调用,也能直接运行。一般的做法是:
def main():
# do all the work
if __name__ = '__main__':
import sys
sys.exit(main())
2
如果这三个脚本和你的例子差不多,你可以用下面这个Python脚本一个接一个地运行它们:
__import__('model-multiple')
import align2d
__import__('model-single')
__import__
是必须的,因为在导入名称中,连字符(-)是不允许的。如果你愿意重命名这些脚本:
import model_multiple
import align2d
import model_single
2
你有两个选择:
- 快速简单的方法:在一个shell脚本或者Python脚本中,把它们一个接一个地调用(可以用
system
或者subprocess.Popen
) - 把它们的工作放在一个函数里,然后把它们导入到一个脚本中,依次调用每个模块的“做工作”函数