Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get environment variable for current user and for all users in Python

Is there way to determine which environment variables returned by os.environ belongs to current user and which one - to all users? I do not want to change it, only get.

UPD: I am using Microsoft Windows 7.

like image 358
Alex G.P. Avatar asked Jun 14 '15 23:06

Alex G.P.


People also ask

How do I see user environment variables in Python?

The os. getenv() method is used to extract the value of the environment variable key if it exists. Otherwise, the default value will be returned. Note: The os module in Python provides an interface to interact with the operating system.

How do you get all environment variables and how can you use them?

You can use any one of the following command to display and list the shell environment variables and their values. The printenv command list the values of the specified environment VARIABLE(s). If no VARIABLE is given, print name and value pairs for them all. printenv command – Print all or part of environment.

How do you find the env value in Python?

getenv() method in Python returns the value of the environment variable key if it exists otherwise returns the default value. default (optional) : string denoting the default value in case key does not exists. If omitted default is set to 'None'.


1 Answers

I don't think you can figure it out using standard Python means like os.environ. The only way to get user and system variables on Windows is to use registry, you can find more here:

HKEY_CURRENT_USER\Environment

System Variables

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

User variables

You need to access registry with Python to get them.

like image 87
Andrey Avatar answered Oct 25 '22 16:10

Andrey