views:

70

answers:

3

I want a small python script to set the HUDSON_HOME environment variable.

When using the shell, I can easily do this using >>set HUDSON_HOME=http://localhost:8080

But how can I do the same directly through python?? I don't want to do it by passing the command line to os.system().. can os.environ() be of any help??

I had in my script: import os os.environ('HUDSON_HOME')='http://localhost:8080'

but it's probably setting it for the subprocss and not the parent shell..any way around this??

+2  A: 

os.environ is a dictionary represenation of the environment. You'd use it like this:

>>> import os
>>> os.environ['HUDSON_HOME'] = 'http://localhost:8080'

However, it cannot modify the environment of the parent process AFAIK.

MattH
A: 

I am unaware of any way to do this as you've requested, as modifying the environment in your Python program will simply change the environment for it, and any child processes, but not the parent process.

That said, if all you need to do is have some Python program that figures out what the value of the variable is, depending on your shell, you should be able to simply assign the output of it to the environment variable:

#!/usr/bin/env python

# code goes here

print 'http://localhost:8080'

If the above was your program, you could run this on the shell, and have HUDSON_HOME set to http://localhost:8080:

$ set HUDSON_HOME=`python program.py`

Note: Those are backticks, which is how it knows to take the output of running the command instead of the command itself.

Joseph Spiros
A: 

thanks Joseph and Matt..Matt, I was making the mistake that u pointed out!! even though it was not able to make changes to the shell, it definitely did work out the way I wanted it to!! if I am giving hudson commands from within this very program it's working fine and that's what I wanted! And Joseph ur's ws a very cute trick!! maybe i'll need it sometime l8r!! thanks guys for such a prompt rep..:-)

Arnab Sen Gupta