python中os.putenv() 没有结果

os.putenv() has no effect



> 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.

https://mail.python.org/pipermail/python-list/2013-June/650169.html

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28713356/viewspace-1145939/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28713356/viewspace-1145939/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值