0

I have been trying to use the sub-process module to run a sequence of shell commands in succession, to automate certain tasks using python(for the first time). The following are the steps involved in the task mentioned before:

  1. Goto location 1
  2. Execute the cshell script in the path location
  3. Goto location 2
  4. Execute another cshell script in the new path location

I have written the following code for the purpose:

#! /usr/bin/python
import subprocess
import os
path = r'/AA/BB/CC/DD/EE/FF/GG/HH/'
os.chdir(path)
p1 = subprocess.Popen('./xyz.csh', stdout=subprocess.PIPE, shell=True)
outdata = p1.communicate()[0]
os.chdir('../XX/YY/')
subprocess.call("./abc.csh")
print("Simulation complete")

The script file contains a few setenv commands which set the environment variables that are required for the execution of abc.csh script. I have the following query about the usage of subprocess.Popen() routine, as the subprocess.Popen() routine creates a sub-shell for the execution of xyz.csh, the environment variables set by the execution of the script vanish once the execution is completed, and abc .csh (which requires those environment variables for execution) throws errors during execution. Is there a way to set environment variables in main shell so that abc.csh can be executed with fail.

  • `environ=os.environ.copy(); environ['some_new_val'] = '1'; Popen(..., env=environ)`? – Torxed Oct 17 '18 at 11:20
  • Possible duplicate of [Python subprocess/Popen with a modified environment](https://stackoverflow.com/questions/2231227/python-subprocess-popen-with-a-modified-environment) – Torxed Oct 17 '18 at 11:20
  • I am new to python, but as understood from the solution the environment copied is already in the path mentioned, but in this case the setenv commands executed within the cshell script sets the env variables in the sub shell, and at the end of execution the shell script, the env variables vanish, which cannot be copied from any path. – Amuly Pandey Oct 17 '18 at 11:36
  • Welcome to stackoverflow btw, don't forget to ping (@Torxed) people if you expect them to respond back, because we don't always get notifications of comments unless they're directed towards us. `subprocess.call()` and `subprocess.Popen()` are two different methods of executing a subprocess. Both of which, won't automatically inherit anything that isn't in the default users shell already. So if you need something else, or share things between them. You need to copy, transfer or supply those values. – Torxed Oct 17 '18 at 11:38
  • @Torxed noted. I understand that I need to copy the sub shell environment to the parent shell. I cannot copy it by providing the path to the first script as given by p1 = subprocess.Popen('./project_setup.csh', env=dict(os.environ, PATH="path")). Did I miss anything? – Amuly Pandey Oct 17 '18 at 11:45

0 Answers0