Reputation: 9537
How can I get system environment variables on Windows? With the below code I only get the user environment variables:
os.environ['PATH']
Or this returns the same:
os.getenv('PATH')
Upvotes: 5
Views: 10093
Reputation: 9537
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]
Upvotes: 3