Python - Shell Environment
April 7, 2006 12:17 PM   Subscribe

Is there a way to set an environment variable in the unix shell from a python script? Can I set a variable in the shell that executes that same script? For that matter, is there a way for any script to do this?

In other words, I am trying to set up a nice environment in my unix shell (potentially bash or tcsh), and I need to set up some custom paths. I would like to do this from a script.

It seems that,

MY_PATH = os.getenv('PATH', 'Error')
os.environ["PATH"] = MY_PATH + ':' + install_path + '/bin'

sets $PATH in some subshell spawned by python, and it doesn't update the environment of the shell that executes the python script.

I had this problem in trying to write a bash script as well.
posted by kuatto to Computers & Internet (8 answers total) 1 user marked this as a favorite
You can do this in bash by "sourcing" the script instead of running it (assuming your current shell is bash too). This means the commands are executed in the current shell, instead of starting a new shell (whose enviroment disappears when the script ends). To source a script, put a period then a space before it, a la:

$ . ./
posted by Capn at 12:20 PM on April 7, 2006

The problem is that, in UNIX each process has its own totally independent set of environment variables. When a shell starts another program, it sets up this new process' environment by copying its own (*), and then that process' if it starts other processes, could in turn pass on any inherited environment variables, but there's no way for these changes to move in the other direction, from parent to child, and there's no way for changes to the parent's environment made after the child is started to effect the child's environment.

(*) actually, it sets up the standard shell variables like PATH, and copies any variables that are "exported" and suchlike.
posted by Capn at 12:26 PM on April 7, 2006

Write your python script to output the path to stdout. Then in your .bash_profile you can backtick it:

export PATH=`python`
posted by majick at 12:30 PM on April 7, 2006

(or whatever the csh equivalent is. Don't ask me, I'm allergic to csh)
posted by majick at 12:32 PM on April 7, 2006

That kind of sucks because then I need a seperate script for each variable I want to set. I am starting to think I might need to set up pipes or something of that sort.
posted by kuatto at 12:40 PM on April 7, 2006

You could have your python program configure the environment you want, and then exec bash at the end. That replaces the current process with bash, and retains the current process's environment.

You'll have to be careful that the exec'd bash doesn't start the python script again, etc, but you can just check for an environment variable it sets in your bashrc and only run it if that variable isn't set.
posted by mendel at 12:48 PM on April 7, 2006

Just have your python script output a series of shell commands and then "eval `python`". You can set any number of variables this way. Or just write your script as a bourne shell script and then "source". This is the standard method of handling initialization of the environment (i.e. ~/.profile and so on are sourced.)

As others have said it's impossible to modify the environment of a parent process from a child.
posted by Rhomboid at 12:52 PM on April 7, 2006

excellent suggestions, thanks!
posted by kuatto at 1:14 PM on April 7, 2006

« Older edit website directly online?   |   how do i ship a heavy 5" binder without a box? Newer »
This thread is closed to new comments.