ayaan ayaan - 1 year ago 151
Bash Question

use setenv in a python subprocess

I've a program where I've to set the environ variable using setenv but I'm getting an error saying that

"/bin/sh: setenv command not found"
but setenv is a built in shell command why is it not working with python subprocess. Can any one help me with this

The code I wrote is:

def ansaunrel(self):
apat = ['setenv ',' ANSA_SRV ','srtia027,srtia028,srtia004']

After setting the env variable I have to run a shell program but the program is not getting started as the environ variable is not set properly. So how can I do this

Answer Source

setenv is a csh builtin, not a bash builtin; you're using the wrong shell if you want to run setenv.

But it doesn't look like you want to run setenv for what you're trying to do. If you want to set the environment for a subprocess, use the env parameter to Popen:

env = dict(os.environ)
env['ANSA_SRV'] = 'srtia027,srtia028,srtia004'
subprocess.Popen(ansrun, shell=True, env=env)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download