ITPub博客

首页 > Linux操作系统 > Linux操作系统 > python中os.putenv() 没有结果

python中os.putenv() 没有结果

原创 Linux操作系统 作者:a871544728 时间:2014-04-21 11:57:00 0 删除 编辑

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博客 ” ,链接:https://blog.itpub.net/28713356/viewspace-1145939/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-07-08

  • 博文量
    28
  • 访问量
    140520