如何访问 Python 中环境变量的当前值

How can I access CURRENT values of environment variables in Python

我知道os.getenvos.environ可以让我读取环境变量的值as of os package import, but I'm writing a program that needs to detect the size of the terminal window dynamically. It would be great to do this without starting a subprocess each time. I am also aware of other solutions(那个页面也给出了子进程的解决方案),但我很好奇关于获取环境变量的 current 值这个看似简单的问题是否有直接的解决方案。

正如 post 提到的那样,有一个仅使用 os 库的解决方案。这是你能得到的最规范的。

一般来说,答案是不可能从一个进程中的另一个进程中获取环境变量的当前值(除非您明确添加一种传达此信息的方式)。当一个进程fork另一个进程时,它可以指定子进程的启动环境,但仅此而已。子进程看不到其父进程对环境所做的任何更改。