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


Groups > comp.lang.python > #48642 > unrolled thread

os.putenv() has no effect

Started byJohannes Bauer <dfnsonfsduifb@gmx.de>
First post2013-06-18 18:49 +0200
Last post2013-06-18 16:31 -0400
Articles 8 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  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

#48642 — os.putenv() has no effect

FromJohannes Bauer <dfnsonfsduifb@gmx.de>
Date2013-06-18 18:49 +0200
Subjectos.putenv() has no effect
Message-ID<kpq32r$cdc$1@news.albasani.net>
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.

Thanks in advance,
Johannes

-- 
>> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
 - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$1@speranza.aioe.org>

[toc] | [next] | [standalone]


#48650

FromJohannes Bauer <dfnsonfsduifb@gmx.de>
Date2013-06-18 19:48 +0200
Message-ID<mailman.3550.1371577713.3114.python-list@python.org>
In reply to#48642
On 18.06.2013 19:24, inq1ltd wrote:

> if you are trying to add a dir to a linux path you need 
> to understand how to add or change environment variables. 

Yeah, about this; I actually am fully aware of what I'm doing.

> research this;
>   $ export PATH= $PATH: ???/???/???

You really couldn't have missed the point more if you tried. I'm not
working with Bash, but with Python. os.putenv() has no effect in my case
(not just with the PATH environment variable).

Regards,
Johannes

[toc] | [prev] | [next] | [standalone]


#48651

Frominq1ltd <inq1ltd@inqvista.com>
Date2013-06-18 13:24 -0400
Message-ID<mailman.3551.1371578736.3114.python-list@python.org>
In reply to#48642

[Multipart message — attachments visible in raw view] — view raw

On Tuesday, June 18, 2013 06:49:31 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:~/b
> in'
> >>> os.putenv("PATH", "/")
> >>> os.getenv("PATH")
> 
> '/usr/joebin:/usr/local/bin:/usr/bin:/bin:/usr/games/bin:/usr/sbin:/sbin:~/b
> in'
> >>> 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.
> 
> Thanks in advance,
> Johannes
> 
> 
if you are trying to add a dir to a linux path you need 
to understand how to add or change environment variables. 
research this;
  $ export PATH= $PATH: ???/???/???

jd
inqvista.com


 



[toc] | [prev] | [next] | [standalone]


#48652

FromDave Angel <davea@davea.name>
Date2013-06-18 14:09 -0400
Message-ID<mailman.3552.1371578965.3114.python-list@python.org>
In reply to#48642
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

[toc] | [prev] | [next] | [standalone]


#48654

FromJohannes Bauer <dfnsonfsduifb@gmx.de>
Date2013-06-18 20:12 +0200
Message-ID<kpq7un$n9o$2@news.albasani.net>
In reply to#48652
On 18.06.2013 20:09, Dave Angel wrote:

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

Huh... this is surprising to me. Because I actually looked it up in the
manual and vaguely remember that there stood that os.environ is just a
copy of the environment variables at interpreter start and not
authorative and (more importantly) that assigning to that dict
would/could somehow lead to memory leaks.

I am extremely certain that I found that passage, but can't find it
right now anymore (probably staring right at it and can't find it still) :-/

Anyways, I'll give it a shot, thank you!

Best regards,
Joe

-- 
>> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
 - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$1@speranza.aioe.org>

[toc] | [prev] | [next] | [standalone]


#48655

FromJohannes Bauer <dfnsonfsduifb@gmx.de>
Date2013-06-18 20:16 +0200
Message-ID<kpq85o$n9o$3@news.albasani.net>
In reply to#48654
On 18.06.2013 20:12, Johannes Bauer wrote:

> I am extremely certain that I found that passage, but can't find it
> right now anymore (probably staring right at it and can't find it still) :-/

Obviously, yes:

Note
On some platforms, including FreeBSD and Mac OS X, setting environ may
cause memory leaks. Refer to the system documentation for putenv.

But still, setting os.environ works, so I'll go with that. Weird that I
read the memory leak part but missed the obvious "so it is actually
preferable to assign to items of os.environ".

Thanks again,
Best regards,
Johannes

-- 
>> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
 - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$1@speranza.aioe.org>

[toc] | [prev] | [next] | [standalone]


#48656

FromSkip Montanaro <skip@pobox.com>
Date2013-06-18 13:22 -0500
Message-ID<mailman.3553.1371579771.3114.python-list@python.org>
In reply to#48642
> Does anybody know why this would happen or what I could be doing wrong?

os.putenv will only affect the environment in subprocesses.  Consider
this session fragment:

% python
Python 2.7.2 (default, Oct 17 2012, 03:11:33)
[GCC 4.4.6 [TWW]] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.putenv("PATH", "/tmp")
>>> os.system("/usr/bin/env")
...
PATH=/tmp
...

Skip

[toc] | [prev] | [next] | [standalone]


#48669

FromTerry Reedy <tjreedy@udel.edu>
Date2013-06-18 16:31 -0400
Message-ID<mailman.3558.1371587520.3114.python-list@python.org>
In reply to#48642
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web