Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python get system environment variables on windows

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!

like image 384
ragesz Avatar asked Oct 19 '22 08:10

ragesz


1 Answers

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]
like image 53
ragesz Avatar answered Nov 01 '22 10:11

ragesz