Using Python to open a shell environment, run a co

2019-02-24 09:54发布

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

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.sh spawns 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.

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.sh it 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.sh does not terminate until one types exit after running desired commands. Using subprocess.call("./env.sh", shell = True) opens the shell and stops there. It is just like entering the command ./env.sh except that when one issues the exit command, the rest of the python script. So:

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

5条回答
趁早两清
2楼-- · 2019-02-24 10:13

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
查看更多
不美不萌又怎样
3楼-- · 2019-02-24 10:18

You can use subprocess:

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

Or you can use os:

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

Here is an example (turn on your speakers):

>>> import os
>>> os.system('say Hello')
查看更多
贼婆χ
4楼-- · 2019-02-24 10:23

subprocess calls (particular Popen) accepts an env argument which is a mapping of environement variables to values. You can use that. e.g.

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.py instead of spawning a whole new process.

查看更多
我想做一个坏孩纸
5楼-- · 2019-02-24 10:25
p = subprocess.Popen(". /path/to/env.sh", shell = True, stdout=subprocess.PIPE).communicate()
subprocess.call("python something.py", shell = True).communicate()
查看更多
叛逆
6楼-- · 2019-02-24 10:27

Here is a help for you.... For running command you could do:

1)

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

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 os module:

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

Or as mentioned you can use import subprocess

查看更多
登录 后发表回答