os.putenv() has no effect

Terry Reedy tjreedy at udel.edu
Tue Jun 18 16:31:35 EDT 2013


On 6/18/2013 12:49 PM, Johannes Bauer wrote:
> Hi group,
>
> I've tracked down a bug in my application to a rather strange
> phaenomenon: os.putenv() doesn't seem to have any effect on my platform
> (x86-64 Gentoo Linux, Python 3.2.3):
>
>>>> os.getenv("PATH")
> '/usr/joebin:/usr/local/bin:/usr/bin:/bin:/usr/games/bin:/usr/sbin:/sbin:~/bin'
>>>> os.putenv("PATH", "/")
>>>> os.getenv("PATH")
> '/usr/joebin:/usr/local/bin:/usr/bin:/bin:/usr/games/bin:/usr/sbin:/sbin:~/bin'
>
>
>>>> os.getenv("FOO")
>>>> os.putenv("FOO", "BAR")
>>>> os.getenv("FOO")
>>>>
>
> Does anybody know why this would happen

 From the doc: "When putenv() is supported, assignments to items in 
os.environ are automatically translated into corresponding calls to 
putenv(); however, calls to putenv() don’t update os.environ, so it is 
actually preferable to assign to items of os.environ."

Also " Such changes to the environment affect subprocesses started with 
os.system(), popen() or fork() and execv()"

Not obvious fact: getenv gets values from the os.environ copy of the 
environment, which is not affected by putenv. See
http://bugs.python.org/issue1159

 > or what I could be doing wrong?

Using putenv(key, value) instead of os.environ[key] = value, which 
suggests that you did not read the full doc entry, which says to use the 
latter ;-).

-- 
Terry Jan Reedy





More information about the Python-list mailing list