import os import ctypes import ctypes.util import posix import pprint def dump(): print("--") os.system("env") print("--") with open("/proc/self/environ", "rb") as f: env = f.read().split(b"\0") pprint.pprint(env) print("==") print() filename = ctypes.util.find_library('c') libc = ctypes.cdll.LoadLibrary(filename) _getenv = libc.getenv _getenv.restype = ctypes.c_char_p _getenv.arg_types = (ctypes.c_char_p,) def getenv(key): key = os.fsencode(key) value = _getenv(key) if value is not None: value = os.fsdecode(value) return value print("[ Start ]") print(os.environ) dump() print("[ Set VAR=VALUE1 ]") os.environ['VAR'] = 'VALUE1' print("get -> %s" % getenv('VAR')) dump() print("[ Set =VALUE2 ]") os.environ[''] = 'VALUE2' print("get -> %s" % getenv('')) dump()