Environment Variables in Python on Linux

前端 未结 5 1137
别那么骄傲
别那么骄傲 2020-12-05 11:53

Python\'s access to environment variables does not accurately reflect the operating system\'s view of the processes environment.

os.getenv and os.environ do not func

5条回答
  •  长情又很酷
    2020-12-05 12:10

    Looking at the Python source code (2.4.5):

    • Modules/posixmodule.c gets the environ in convertenviron() which gets run at startup (see INITFUNC) and stores the environment in a platform-specific module (nt, os2, or posix)

    • Lib/os.py looks at sys.builtin_module_names, and imports all symbols from either posix, nt, or os2

    So yes, it gets decided at startup. os.environ is not going to be helpful here.

    If you really want to do this, then the most obvious approach that comes to mind is to create your own custom C-based python module, with a getenv that always invokes the system call.

提交回复
热议问题