Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #48652

Re: os.putenv() has no effect

Date 2013-06-18 14:09 -0400
From Dave Angel <davea@davea.name>
Subject Re: os.putenv() has no effect
References <kpq32r$cdc$1@news.albasani.net>
Newsgroups comp.lang.python
Message-ID <mailman.3552.1371578965.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 06/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 or what I could be doing wrong?
> Help is greatly appreciated.
>

Quoting (retyping) from the getenv docs, "...however, calls to putenv() 
don't update os.environ, so it is actually preferable to assign to items 
of os.environ."

As to why, I'm not at all sure.  Only that many environments don't 
support putenv().  But why that should stop it working in the obvious 
way ?  No idea.

os.environ is not an ordinary dict, it's a "mapping object".  And among 
other things, when you modify os.environ, Python will call putenv. 
Quoting from the os.environ docs, "If the platform supports the putenv() 
function, this mapping may be used to modify the environment.  putenv() 
will be called automatically wehn the mapping is modified."

In other words, you shouldn't use putenv(), but instead modify os.environ.

-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

os.putenv() has no effect Johannes Bauer <dfnsonfsduifb@gmx.de> - 2013-06-18 18:49 +0200
  Re: os.putenv() has no effect Johannes Bauer <dfnsonfsduifb@gmx.de> - 2013-06-18 19:48 +0200
  Re: os.putenv() has no effect inq1ltd <inq1ltd@inqvista.com> - 2013-06-18 13:24 -0400
  Re: os.putenv() has no effect Dave Angel <davea@davea.name> - 2013-06-18 14:09 -0400
    Re: os.putenv() has no effect Johannes Bauer <dfnsonfsduifb@gmx.de> - 2013-06-18 20:12 +0200
      Re: os.putenv() has no effect Johannes Bauer <dfnsonfsduifb@gmx.de> - 2013-06-18 20:16 +0200
  Re: os.putenv() has no effect Skip Montanaro <skip@pobox.com> - 2013-06-18 13:22 -0500
  Re: os.putenv() has no effect Terry Reedy <tjreedy@udel.edu> - 2013-06-18 16:31 -0400

csiph-web