Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

shell script through Python

Status
Not open for further replies.

kmarris

Programmer
Jan 25, 2007
12
0
0
US
I have a LINUX bash shell script that sets environment variables. I need to execute this script through Python. I've tried to use os.system and subprocess.call, but each time, the shell script seems to modify the environment variables in a subshell and exits.

How can I run a LINUX shell script through Python so that it modifies the current environment?

Thanks,
Kevin
 
Several possible solutions come to mind, but the easiest to implement is reverse your idea of having the Python script call your Bash script. Instead have your Bash script call your Python script once it has setup the environmental variables you want.

Alternatively, if there are not too many of them, you could set the environmental variables on the command line as you invoke your Python script, for example:

TMPDIR=/tmp LANG=C /home/yours/bin/yourpython_script

Get the idea?

 
Thanks for your reply. However, I was looking for a solution in line with the problem I presented. My requirements are that the Python script needs to execute a shell script to set environment variables. The environment variable list is dynamic, so I cannot simply set the variables on the command line. Otherwise, I would just use os.environ['VAR']=value in the Python script.

If anyone knows if it possible to execute a LINUX shell script through Python so that it modifies the current environment, can you please provide a documentation link or sample code proving it?
 
Since the environmental variables are dynamic, but somehow the shell script adjusts for them, why not use the first suggestion I gave you? I don't know what your shell script looks like, so will make up an example here:

Code:
#!/bin/bash
export TMPDIR=/tmp LANG=C
/path/to/your/python_script
exit 0

Maybe it would help if you stated the problem differently or show some of your code?

Are you using environmental variables to pass information to the Python script? Or are you trying to have the Python script setup the environmental variable for some other process?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top