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/
本文探讨了Python中os.putenv()在某些环境下不起作用的现象,并解释了如何通过修改os.environ来正确设置环境变量。
1万+

被折叠的 条评论
为什么被折叠?



