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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With