如何使用另一个python脚本文件中的参数执行python脚本文件 [英] how to execute a python script file with an argument from inside another python script file

查看:105
本文介绍了如何使用另一个python脚本文件中的参数执行python脚本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我想从另一个 python 文件中执行一个带有参数的 python 文件以获取返回值....

my problem is that I want to execute a python file with an argument from inside another python file to get the returned values....

我不知道我是否解释得很好...

I don't know if I've explained it well...

示例:

从我执行这个shell:

from the shell I execute this:

          getCameras.py "path_to_the_scene"

这会返回一个摄像机列表....

and this return me a list of cameras....

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

so how can I call this script (including the argument) from another script ???

我一直试图通过阅读这里的其他一些问题来自己弄明白,但我没有弄明白,我应该使用 execfile() 函数吗??究竟如何??

I've been trying to figure it out by myself by reading some other questions here , but I didn't get it well, should I use the execfile() function?? how exactly??

提前感谢帮助像我这样的新手!!

Thanks in advance for helping a newbie like me!!

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

Ok, after take a look at your answers, I have to edit my question to make it more concise and because I don't understand some answers(sorry, like I said I'm a newbie!!!):

好吧,我有这 2 个脚本getMayaCameras.py"和doRender.py",还有一个名为renderUI.py"的脚本,它在 GUI 中实现了前 2 个脚本.

Well, I have this 2 scripts "getMayaCameras.py" and "doRender.py" and one more called "renderUI.py" that implements the first 2 scripts in a GUI.

getMayaCameras.py"和doRender.py"都是可以通过添加参数(或标志,在doRender.py"情况下)直接从系统外壳执行的脚本,如果可能,我想仍然有这种可能性,所以我可以在执行 UI 或直接从 shell 执行脚本之间进行选择

"getMayaCameras.py" and "doRender.py" are both scipts that you can execute directly from the system shell by adding an argument ( or flags, in the "doRender.py" case) and, If it is possible, I want to still having this posibility so I can choose between execute the UI or execute the script dirctly from the shell

我已经通过从renderUI.py"脚本中导入它们来为它们进行了一些修改,但现在它们不能自己工作......

I've made already some modifications for them to work by importing them from the "renderUI.py" script but now they don't work by themselves....

那么是否可以让这些脚本自己工作,并且仍然可以从另一个脚本调用它们?究竟如何?您之前告诉我的这个 将逻辑与命令行参数处理分离" 对我来说听起来不错,但我不知道如何在我的脚本中实现它(我尝试过但没有成功)....

So is possible to have this scripts working by themselves and still having the possiblity of calling them from another script? how exactly? This "separating the logic from the command line argument handling" that you told me before sounds good to me but I don't know how to implement it on my script ( I tried but without succes) ....

这就是为什么我在此处发布原始代码供您查看我是如何制作的,请随时提出批评和/或更正代码以解释我应该如何使其脚本正常工作...

That's why I'm posting here the original code for you to see how I made it, feel free both to make critics and/or correct the code to explain me how should I make it for the script to work properly...

#!/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()

再次感谢,

大卫

推荐答案

最好的答案是不要.将您的 getCameras.py 编写为

The best answer is don't. Write your getCameras.py as

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]))

从你的其他脚本,你可以做

From your other script, you can then do

import getCamera

getCamera.main(arg1, arg2)

或调用 getCamera.py 中的任何其他函数

or call any other functions in getCamera.py

这篇关于如何使用另一个python脚本文件中的参数执行python脚本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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