使用Python打开shell环境,运行命令退出环境

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22163422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:24:19  来源:igfitidea点击:

Using Python to open a shell environment, run a command and exit environment

pythonbashshellsubprocess

提问by user3377586

I'm trying to automate a process using python. If I am just in the terminal the workflow looks like:

我正在尝试使用 python 自动化一个过程。如果我只是在终端中,工作流程如下所示:

user:> . /path/to/env1.sh
user:> python something.py
user:> exit
user:> . /path/to/env2.sh
user:> python something2.py
user:> exit 

etc for a few more steps. Each env.shspawns a new script with a whole slew of environment variables and whatnot set within the current directory. I'm pretty sure I need to use subprocess, but I'm not exactly sure how to go about it. Ideally the workflow would go: open new shell --> run some commands --> exit shell --> repeat as necessary.

等几个步骤。每个都会env.sh产生一个新脚本,其中包含大量环境变量以及当前目录中未设置的内容。我很确定我需要使用子流程,但我不确定如何去做。理想情况下,工作流程应该是:打开新外壳 --> 运行一些命令 --> 退出外壳 --> 根据需要重复。

EDIT: It seems some clarification is needed. I understand how to use subprocess.Popen()and subprocess.call()to call things from within the shell that the Python script was called from. This is not what I need. When one calls env.shit sets a whole ton of environment variables and a few other pertinent things and then drops you into a shell to run commands. It is important to note env.shdoes not terminate until one types exitafter running desired commands. Using subprocess.call("./env.sh", shell = True)opens the shell and stops there. It is just like entering the command ./env.shexcept that when one issues the exitcommand, the rest of the python script. So:

编辑:似乎需要一些澄清。我了解如何从调用 Python 脚本的 shell 中使用subprocess.Popen()subprocess.call()调用事物。这不是我需要的。当一个人调用env.sh它时,它会设置一大堆环境变量和一些其他相关的东西,然后让你进入一个 shell 来运行命令。重要的是要注意在运行所需命令后env.sh直到一种类型才终止exit。使用subprocess.call("./env.sh", shell = True)打开外壳并在那里停止。就像输入命令一样,./env.sh除了当发出exit命令时,python 脚本的其余部分。所以:

subprocess.call(". /path/to/env.sh", shell = True)
subprocess.call("python something.py", shell = True)

Does NOT do what I need it to do, nor does:

不做我需要它做的事情,也不做:

p = subprocess.Popen(". /path/to/env.sh", shell = True)
subprocess.call("python something.py", shell = True)
p.kill()

采纳答案by jfs

As I understand you want to run a command and then pass it other commands:

据我了解,您想运行一个命令,然后将其他命令传递给它:

from subprocess import Popen, PIPE

p = Popen("/path/to/env.sh", stdin=PIPE)   # set environment, start new shell
p.communicate("python something.py\nexit") # pass commands to the opened shell

回答by A.J. Uppal

You can use subprocess:

您可以使用subprocess

>>> import subprocess
>>> subprocess.call('python something.py', shell = True)

Or you can use os:

或者你可以使用os

>>> import os
>>> os.system('python something.py')

Here is an example (turn on your speakers):

这是一个示例(打开扬声器):

>>> import os
>>> os.system('say Hello')

回答by mgilson

subprocesscalls (particular Popen) accepts an envargument which is a mapping of environement variables to values. You can use that. e.g.

subprocess调用(特别Popen)接受一个env参数,它是环境变量到值的映射。你可以用那个。例如

env = {'FOO': 'Bar', 'HOME': '/path/to/home'}
process = subprocess.Popen(['python', 'something.py'], env=env)

Of course, usually, it's better to just call some functions after *import*ing something.pyinstead of spawning a whole new process.

当然,通常情况下,最好在 *import*ing 之后调用一些函数,something.py而不是产生一个全新的进程。

回答by MLSC

Here is a helpfor you.... For running command you could do:

这是对您的帮助....对于运行命令,您可以执行以下操作:

1)

1)

from subprocess import call
call(["ls", "-l"])

2)

2)

import os
os.system("command")

Example:

例子:

import os
f = os.popen('date')
now = f.read()
print "Today is ", now

For enabling terminal you can import osmodule:

要启用终端,您可以导入os模块:

import os
os.system('python script.py')

Or as mentioned you can use import subprocess

或者如前所述,您可以使用 import subprocess

回答by user3273866

p = subprocess.Popen(". /path/to/env.sh", shell = True, stdout=subprocess.PIPE).communicate()
subprocess.call("python something.py", shell = True).communicate()