简体   繁体   English

在 Python shell 中执行带参数的文件

[英]Execute a file with arguments in Python shell

I would like to run a command in Python Shell to execute a file with an argument.我想在 Python Shell 中运行一个命令来执行一个带参数的文件。

For example: execfile("abc.py") but how to add 2 arguments?例如: execfile("abc.py")但是如何添加 2 个参数?

try this:尝试这个:

import sys
sys.argv = ['arg1', 'arg2']
execfile('abc.py')

Note that when abc.py finishes, control will be returned to the calling program.请注意,当abc.py完成时,控制权将返回给调用程序。 Note too that abc.py can call quit() if indeed finished.还要注意,如果确实完成了abc.py可以调用quit()

Actually, wouldn't we want to do this?实际上,我们不想这样做吗?

import sys
sys.argv = ['abc.py','arg1', 'arg2']
execfile('abc.py')

execfile runs a Python file, but by loading it, not as a script. execfile运行 Python 文件,但通过加载它,而不是作为脚本。 You can only pass in variable bindings, not arguments.您只能传入变量绑定,不能传入参数。

If you want to run a program from within Python, use subprocess.call .如果要从 Python 中运行程序,请使用subprocess.call Eg例如

import subprocess
subprocess.call(['./abc.py', arg1, arg2])
import sys
import subprocess

subprocess.call([sys.executable, 'abc.py', 'argument1', 'argument2'])

For more interesting scenarios, you could also look at the runpy module.对于更有趣的场景,您还可以查看runpy模块。 Since python 2.7, it has the run_path function.从 python 2.7 开始,它具有run_path函数。 Eg:例如:

import runpy
import sys

# argv[0] will be replaced by runpy
# You could also skip this if you get sys.argv populated
# via other means
sys.argv = ['', 'arg1' 'arg2']
runpy.run_path('./abc.py', run_name='__main__')

You're confusing loading a module into the current interpreter process and calling a Python script externally.您混淆了将模块加载到当前解释器进程中并在外部调用 Python 脚本。

The former can be done by import ing the file you're interested in. execfile is similar to importing but it simply evaluates the file rather than creates a module out of it.前者可以通过import您感兴趣的文件来完成。 execfile类似于导入,但它只是评估文件而不是从中创建模块。 Similar to "sourcing" in a shell script.类似于 shell 脚本中的“采购”。

The latter can be done using the subprocess module.后者可以使用 subprocess 模块来完成。 You spawn off another instance of the interpreter and pass whatever parameters you want to that.您生成另一个解释器实例并传递您想要的任何参数。 This is similar to shelling out in a shell script using backticks.这类似于在 shell 脚本中使用反引号进行炮击。

You can't pass command line arguments with execfile() .您不能使用execfile()传递命令行参数。 Look at subprocess instead.改为查看subprocess

If you set PYTHONINSPECT in the python file you want to execute如果在要执行的python文件中设置了PYTHONINSPECT

[repl.py] [复制.py]

import os
import sys
from time import time 
os.environ['PYTHONINSPECT'] = 'True'
t=time()
argv=sys.argv[1:len(sys.argv)]

there is no need to use execfile , and you can directly run the file with arguments as usual in the shell:不需要使用execfile ,您可以像往常一样在 shell 中使用参数直接运行文件:

python repl.py one two 3
>>> t
1513989378.880822
>>> argv
['one', 'two', '3']

If you want to run the scripts in parallel and give them different arguments you can do like below.如果你想并行运行脚本并给它们不同的参数,你可以像下面那样做。

import os
os.system("python script.py arg1 arg2 & python script.py arg11 arg22")

This works for me :这对我有用:

import subprocess
subprocess.call(['python.exe', './abc.py', arg1, arg2])

Besides subprocess.call , you can also use subprocess.Popen .除了subprocess.call ,您还可以使用subprocess.Popen Like the following像下面这样

subprocess.Popen(['./script', arg1, arg2])

这有效:

subprocess.call("python abc.py arg1 arg2", shell=True)
runfile('abc.py', ['arg1', 'arg2'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM