Python\'s access to environment variables does not accurately reflect the operating system\'s view of the processes environment.
os.getenv and os.environ do not func
I don't believe many programs EVER expect to have their environment externally modified, so loading a copy of the passed environment at startup is equivalent. You have simply stumbled on an implementation choice.
If you are seeing all the set-at-startup values and putenv/setenv from within your program works, I don't think there's anything to be concerned about. There are far cleaner ways to pass updated information to running executables.
Looking at the Python source code (2.4.5):
Modules/posixmodule.c gets the environ in convertenviron() which gets run at startup (see INITFUNC) and stores the environment in a platform-specific module (nt, os2, or posix)
Lib/os.py looks at sys.builtin_module_names, and imports all symbols from either posix, nt, or os2
So yes, it gets decided at startup. os.environ is not going to be helpful here.
If you really want to do this, then the most obvious approach that comes to mind is to create your own custom C-based python module, with a getenv that always invokes the system call.
That's a very good question.
It turns out that the os
module initializes os.environ
to the value of posix.environ
, which is set on interpreter start up. In other words, the standard library does not appear to provide access to the getenv function.
That is a case where it would probably be safe to use ctypes on unix. Since you would be calling an ultra-standard libc function.
You can use ctypes
to do this pretty simply:
>>> from ctypes import CDLL, c_char_p
>>> getenv = CDLL("libc.so.6").getenv
>>> getenv.restype = c_char_p
>>> getenv("HOME")
'/home/glyph'
Another possibility is to use pdb, or some other python debugger instead, and change os.environ at the python level, rather than the C level. Here's a small recipe I posted to interrupt a running python process and provide access to a python console on receiving a signal. Alternatively, just stick a pdb.set_trace() at some point in your code you want to interrupt. In either case, just run the statement "import os; os.environ['SOME_VARIABLE']='my_value'
" and you should be updated as far as python is concerned.
I'm not sure if this will also update the C environment with setenv, so if you have C modules using getenv directly you may have to do some more work to keep this in sync.