2010-11-20 238 views
14

我的问题是我想用另一个python文件中的参数执行python文件以获取返回的值....如何使用另一个python脚本文件中的参数执行python脚本文件

我不知道我是否很好地解释它...

例如:

从壳我执行此:

  getCameras.py "path_to_the_scene" 

,这回我的摄像机列表.. ..

那么我怎样才能从另一个脚本调用这个脚本(包括参数)???

我一直想通过阅读其他一些问题来解决这个问题,但是我没搞清楚,应该使用execfile()函数吗?如何?

预先感谢帮助像我这样的新手!

好的,看看你的答案后,我必须编辑我的问题,使它更简洁,因为我不明白一些答案(对不起,就像我说我是新手!!!):

嗯,我有这2个脚本“getMayaCameras.py”和“doRender.py”和一个叫做“renderUI.py”,它实现了GUI中的前两个脚本。

“getMayaCameras.py”和“doRender.py”都是可以通过添加参数(或标志,在“doRender.py”情况下)直接从系统shell执行的摘要,如果可能,我想仍然有这种可能性,所以我可以选择执行用户界面或从shell直接执行脚本

我已经做了一些修改,使他们能够通过从“renderUI.py”脚本中导入它们进行工作但现在它们不能自行工作......

所以有可能让这些脚本自己工作,仍然有可能从另一个脚本调用它们吗?究竟如何?这“分开逻辑从命令行参数处理”,你告诉我之前听起来不错,但我不知道如何执行它在我的脚本(我试过,但没有成功)....

这就是为什么我在这里发布原始代码给你,看看我是如何做到这一点的,既可以让评论家也可以和/或更正代码来解释我应该如何让脚本正常工作......

再次
#!/usr/bin/env python 

import re,sys 

if len(sys.argv) != 2: 
    print 'usage : getMayaCameras.py <path_to_originFile> \nYou must specify the path to the origin file as the first arg' 
    sys.exit(1) 


def getMayaCameras(filename = sys.argv[1]): 
    try: 
     openedFile = open(filename, 'r') 
    except Exception: 
     print "This file doesn't exist or can't be read from" 
     import sys 
     sys.exit(1) 

    cameras = []  
    for line in openedFile: 
     cameraPattern = re.compile("createNode camera")  
     cameraTest = cameraPattern.search(line) 
     if cameraTest:  
      cameraNamePattern = re.compile("-p[\s]+\"(.+)\"")   
      cameraNameTest = cameraNamePattern.search(line)   
      name = cameraNameTest.group(1)   
      cameras.append(name)    
    openedFile.close() 

    return cameras  

getMayaCameras() 

感谢,

大卫

回答

39

最好的答案是不要。写下你的getCameras。PY作为

import stuff1 
import stuff2 
import sys 

def main(arg1, arg2): 
    # do whatever and return 0 for success and an 
    # integer x, 1 <= x <= 256 for failure 

if __name__=='__main__': 
    sys.exit(main(sys.argv[1], sys.argv[2])) 

从其他脚本,然后你可以做

import getCamera 

getCamera.main(arg1, arg2) 

或调用任何其它功能getCamera.py

+1

我不明白这一点,用这种方法,你仍然可以用shell中的args调用脚本吗?无论如何,我试图在我的脚本中实现它没有成功,请你能做到这一点用我留在这里的脚本,我可以用它作为例子吗? – user497457 2010-11-20 23:02:54

+0

不起作用... – user1701047 2013-03-12 16:30:25

0

execfile()在另一个脚本内运行一个脚本,这不是你想要的。 subprocess模块可以用来运行Python解释器的另一个实例,但是您应该做的是查看getCameras.py并查看是否有一些可以在导入之后调用的函数。

9

首先,我同意其他人,你应该编辑从命令行参数处理中分离逻辑的代码。

但是,如果您使用其他库并且不想乱编译它们,那么知道如何在Python中执行等价的命令行工作仍然很有用。
解决方案是os.system(命令)
至少在Windows上,它启动一个控制台并执行该命令,就像您将它输入命令提示符一样。

import os 
os.system('getCameras.py "path_to_the_scene" ') 
+1

感谢您的答案,但我不能得到它的工作,至少如果我做像camera = os.system('getCameras.py“path_to_the_scene”').....它没有得到返回的值 – user497457 2010-11-20 20:16:32

0

我建议你重新组织你的getCameras.py,在get_cameras()方法中包装获取相机列表代码。然后你可以在其他python脚本中调用这个方法。

getCameras.py

def get_cameras(): 
bulabula... 
if __name__ == '__main__': 
return get_cameras() 

使用方法:other.py

import getCameras 
camera_list = getCameras.get_cameras() 
2

另一种方式可能是优选的使用os.system()将是使用所述subprocess模块,其被发明,以取代os.system()以及其他一些稍旧的模块。用下面的程序是一个你想与一些主要的程序来调用:

import argparse 

# Initialize argument parse object 
parser = argparse.ArgumentParser() 

# This would be an argument you could pass in from command line 
parser.add_argument('-o', action='store', dest='o', type=str, required=True, 
        default='hello world') 

# Parse the arguments 
inargs = parser.parse_args() 
arg_str = inargs.o 

# print the command line string you passed (default is "hello world") 
print(arg_str) 

使用上述程序从一个主程序subproccess就应该是这样的:

import subprocess 

# run your program and collect the string output 
cmd = "python your_program.py -o THIS STRING WILL PRINT" 
out_str = subprocess.check_output(cmd, shell=True) 

# See if it works. 
print(out_str) 

在年底这一天将打印"THIS STRING WILL PRINT",这是你通过我所谓的主程序。 subprocess有很多选项,但值得使用,因为如果你使用它,写你的程序将是系统独立的。请参阅subprocessargparse的文档。

相关问题