gpt4 book ai didi

python - UID env-var 未复制到 Python 的 'os.environ' dict

转载 作者:太空宇宙 更新时间:2023-11-03 14:49:35 25 4
gpt4 key购买 nike

我有一个 Python 脚本需要知道 $UID 环境变量的值。问题是,出于某种原因,这个变量在 os.environ 中不存在。

下面是一个交互式演示 - $UID 是在 shell 上定义的,但是虽然 Python 是从同一个 shell 启动的,但是 os.environ 中不存在 'UID' 环境变量。

这是一个已知问题吗?有什么办法吗?

$ echo $UID
1003
$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['UID']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__
raise KeyError(key)
KeyError: 'UID'
>>>

最佳答案

这是预期的,因为 UIDshell internal variable .它不会传播到子流程。

您必须使用 os.getuid() 直接通过 os 模块获取值

关于python - UID env-var 未复制到 Python 的 'os.environ' dict,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46805278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com