如何从另一个 python 文件中调用需要命令行参数的 python 文件?
How do you call a python file that requires a command line argument from within another python file?
例如,我有两个 python 文件,'test1.py'
和 'test2.py'
。我想把import test2
变成test1
,这样当我运行test1
的时候,它也运行变成了test2
。
但是,为了 运行 正确,test2
需要一个输入参数。通常,当我从 test1
外部 运行 test2
时,我只是在 command line
中的文件调用之后键入参数。从 test1
中调用 test2
时如何完成此操作?
根据编辑能力test2.py有两种选择:
- (可编辑)将test2.py内容打包成class并在init.
中传递args
在test1.py文件中:
from test2 import test2class
t2c = test2class(neededArgumetGoHere)
t2c.main()
在test2.py文件中:
class test2class:
def __init__(self, neededArgumetGoHere):
self.myNeededArgument = neededArgumetGoHere
def main(self):
# do stuff here
pass
# to run it from console like a simple script use
if __name__ == "__main__":
t2c = test2class(neededArgumetGoHere)
t2c.main()
- (无法编辑 test2.py) 运行 test2.py 作为子流程。查看子流程 docs 了解更多信息,了解如何使用它。
test1.py
from subprocess import call
call(['path/to/python','test2.py','neededArgumetGoHere'])
假设您可以定义自己的 test1 和 test2,并且您可以使用 argparse(无论如何这是个好主意):
使用 argparse 的好处是您可以让 test2 定义一大堆默认参数值,而 test1 不必担心。而且,在某种程度上,您有一个用于 test2 调用的文档化接口。
抄袭https://docs.python.org/2/howto/argparse.html
test2.py
import argparse
def get_parser():
"separate out parser definition in its own function"
parser = argparse.ArgumentParser()
parser.add_argument("square", help="display a square of a given number")
return parser
def main(args):
"define a main as the test1=>test2 entry point"
print (int(args.square)**2)
if __name__ == '__main__':
"standard test2 from command line call"
parser = get_parser()
args = parser.parse_args()
main(args)
audrey:探索 jluc$ python test2.py 3
9
test1.py
import test2
import sys
#ask test2 for its parser
parser = test2.get_parser()
try:
#you can use sys.argv here if you want
square = sys.argv[1]
except IndexError:
#argparse expects strings, not int
square = "5"
#parse the args for test2 based on what test1 wants to do
#by default parse_args uses sys.argv, but you can provide a list
#of strings yourself.
args = parser.parse_args([square])
#call test2 with the parsed args
test2.main(args)
audrey:探索 jluc$ python test1.py 6
36
audrey:探索 jluc$ python test1.py
25
您可以使用 subprocess 模块中的 call 或 popen 方法。
from subprocess import call, Popen
Call(file, args)
Popen(file args)
例如,我有两个 python 文件,'test1.py'
和 'test2.py'
。我想把import test2
变成test1
,这样当我运行test1
的时候,它也运行变成了test2
。
但是,为了 运行 正确,test2
需要一个输入参数。通常,当我从 test1
外部 运行 test2
时,我只是在 command line
中的文件调用之后键入参数。从 test1
中调用 test2
时如何完成此操作?
根据编辑能力test2.py有两种选择:
- (可编辑)将test2.py内容打包成class并在init. 中传递args
在test1.py文件中:
from test2 import test2class
t2c = test2class(neededArgumetGoHere)
t2c.main()
在test2.py文件中:
class test2class:
def __init__(self, neededArgumetGoHere):
self.myNeededArgument = neededArgumetGoHere
def main(self):
# do stuff here
pass
# to run it from console like a simple script use
if __name__ == "__main__":
t2c = test2class(neededArgumetGoHere)
t2c.main()
- (无法编辑 test2.py) 运行 test2.py 作为子流程。查看子流程 docs 了解更多信息,了解如何使用它。
test1.py
from subprocess import call
call(['path/to/python','test2.py','neededArgumetGoHere'])
假设您可以定义自己的 test1 和 test2,并且您可以使用 argparse(无论如何这是个好主意):
使用 argparse 的好处是您可以让 test2 定义一大堆默认参数值,而 test1 不必担心。而且,在某种程度上,您有一个用于 test2 调用的文档化接口。
抄袭https://docs.python.org/2/howto/argparse.html
test2.py
import argparse
def get_parser():
"separate out parser definition in its own function"
parser = argparse.ArgumentParser()
parser.add_argument("square", help="display a square of a given number")
return parser
def main(args):
"define a main as the test1=>test2 entry point"
print (int(args.square)**2)
if __name__ == '__main__':
"standard test2 from command line call"
parser = get_parser()
args = parser.parse_args()
main(args)
audrey:探索 jluc$ python test2.py 3
9
test1.py
import test2
import sys
#ask test2 for its parser
parser = test2.get_parser()
try:
#you can use sys.argv here if you want
square = sys.argv[1]
except IndexError:
#argparse expects strings, not int
square = "5"
#parse the args for test2 based on what test1 wants to do
#by default parse_args uses sys.argv, but you can provide a list
#of strings yourself.
args = parser.parse_args([square])
#call test2 with the parsed args
test2.main(args)
audrey:探索 jluc$ python test1.py 6
36
audrey:探索 jluc$ python test1.py
25
您可以使用 subprocess 模块中的 call 或 popen 方法。
from subprocess import call, Popen
Call(file, args)
Popen(file args)