Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

os.putenv() has no effect

776 views
Skip to first unread message

Johannes Bauer

unread,
Jun 18, 2013, 12:49:31 PM6/18/13
to
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>

Johannes Bauer

unread,
Jun 18, 2013, 1:48:28 PM6/18/13
to pytho...@python.org
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

inq1ltd

unread,
Jun 18, 2013, 1:24:50 PM6/18/13
to pytho...@python.org, Johannes Bauer

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

 

 

 

 

 

 

Dave Angel

unread,
Jun 18, 2013, 2:09:11 PM6/18/13
to pytho...@python.org
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

Johannes Bauer

unread,
Jun 18, 2013, 2:12:39 PM6/18/13
to
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

Johannes Bauer

unread,
Jun 18, 2013, 2:16:24 PM6/18/13
to
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

Skip Montanaro

unread,
Jun 18, 2013, 2:22:48 PM6/18/13
to Johannes Bauer, pytho...@python.org
> 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

Terry Reedy

unread,
Jun 18, 2013, 4:31:35 PM6/18/13
to pytho...@python.org
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


0 new messages