os.environ() giving errors while setting for Hudson
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??
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.
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.
精彩评论