How can I get system environment variables on windows? With the below codes I only get the user environment variables:
os.environ['PATH']
Or this returns the same:
os.getenv('PATH')
Thank you!
解决方案
Based on a (deleted) comment I found the solution. System environment variables should be read from the registry if the python script is run by a user and not by administrator.
import winreg
reg_path = r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
reg_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_path)
system_environment_variables = winreg.QueryValueEx(reg_key, 'Path')[0]