本文翻译自:Run a Python script from another Python script, passing in arguments [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
I want to run a Python script from another Python script. 我想从另一个Python脚本运行一个Python脚本。 I want to pass variables like I would using the command line. 我想像使用命令行一样传递变量。
For example, I would run my first script that would iterate through a list of values (0,1,2,3) and pass those to the 2nd script script2.py 0
then script2.py 1
, etc. 例如,我将运行我的第一个脚本,该脚本将遍历值列表(0,1,2,3)并将其传递给第二个脚本script2.py 0
然后script2.py 1
等。
I found Stack Overflow question 1186789 which is a similar question, but ars's answer calls a function, where as I want to run the whole script, not just a function, and balpha's answer calls the script but with no arguments. 我发现了Stack Overflow问题1186789 ,这是一个类似的问题,但是ars的答案调用了一个函数,在这里我想运行整个脚本,而不仅仅是函数,而balpha的答案则调用了脚本,但没有参数。 I changed this to something like the below as a test: 我将其更改为以下内容作为测试:
execfile("script2.py 1")
But it is not accepting variables properly. 但是它没有正确接受变量。 When I print out the sys.argv
in script2.py it is the original command call to first script "['C:\\script1.py']. 当我在script2.py中打印sys.argv
,它是对第一个脚本“ ['C:\\ script1.py']的原始命令调用。
I don't really want to change the original script (ie script2.py in my example) since I don't own it. 我真的不想更改原始脚本(例如,在我的示例中为script2.py),因为我不拥有它。
I figure there must be a way to do this; 我认为必须有一种方法可以做到这一点; I am just confused how you do it. 我只是很困惑你怎么做。
#1楼
参考:https://stackoom.com/question/fRPB/从另一个Python脚本运行一个Python脚本-并传入参数-重复
#2楼
import subprocess
subprocess.call(" python script2.py 1", shell=True)
#3楼
I think the good practice may be something like this; 我认为好的做法可能是这样的;
import subprocess
cmd = 'python script.py'
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
out, err = p.communicate()
result = out.split('\n')
for lin in result:
if not lin.startswith('#'):
print(lin)
according to documentation The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. 根据文档,子流程模块允许您生成新流程,连接到其输入/输出/错误管道并获取其返回代码。 This module intends to replace several older modules and functions: 该模块打算替换几个较旧的模块和功能:
os.system
os.spawn*
os.popen*
popen2.*
commands.*
Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process. 使用communication()而不是.stdin.write,.stdout.read或.stderr.read来避免死锁,这是由于其他任何OS管道缓冲区中的任何一个填满并阻塞了子进程造成的。 Read Here 在这里阅读
#4楼
Try using os.system
: 尝试使用os.system
:
os.system("script2.py 1")
execfile
is different because it is designed to run a sequence of Python statements in the current execution context. execfile
有所不同,因为它旨在在当前执行上下文中运行一系列Python语句。 That's why sys.argv
didn't change for you. 这就是sys.argv
不会为您更改的原因。
#5楼
SubProcess module: 子流程模块:
http://docs.python.org/dev/library/subprocess.html#using-the-subprocess-module http://docs.python.org/dev/library/subprocess.html#using-the-subprocess-module
import subprocess
subprocess.Popen("script2.py 1", shell=True)
With this, you can also redirect stdin, stdout, and stderr. 这样,您还可以重定向stdin,stdout和stderr。
#6楼
如果os.system对您来说功能不足,那么可以使用subprocess 模块 。