简体   繁体   中英

How can I access CURRENT values of environment variables in Python

I know that os.getenv and os.environ will allow me to read the values of environment variables 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 (that page also gives the sub-process solution), but I am curious about whether there is a direct solution to this simple-seeming problem of getting the current value of an environment variable.

In general, the answer is that it is not possible to get the current value of an environment variable in one process from another process (unless you explicitly add a way of communicating this info). When one process forks another process, it can specify the startup environment of the child process, but that is as far as it goes. The child process does not see any changes in the environment made by its parent.

As the alluded to post mentions, there is a solution using just the os library. That's about as canonical as you can get.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM