6

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.

Alex G.P.
  • 9,609
  • 6
  • 46
  • 81
  • 1
    What operating system are you running? – Simeon Visser Jun 14 '15 at 23:17
  • 1
    It's `os.environ` not `os.environ()` – maxymoo Jun 14 '15 at 23:27
  • @maxymoo [No](https://docs.python.org/3/library/os.html#os.environ). –  Jun 15 '15 at 09:11
  • What do you mean by "belongs"? How does an environment variable belong to a user or to all users? –  Jun 15 '15 at 09:13
  • @Tichodroma there is such thing on Windows – Andrey Jun 15 '15 at 09:16
  • @Andrey What do you mean by "such thing"? –  Jun 15 '15 at 09:17
  • @Tichodroma distinction between env variables for user and for all users. – Andrey Jun 15 '15 at 09:17
  • @Andrey So the question of the OP makes no sense. –  Jun 15 '15 at 09:18
  • @Tichodroma it makes total sense. If you get a value via `os.environ` you don't know whether this value belongs to this user or to all users. – Andrey Jun 15 '15 at 09:20
  • @Tichodroma on Windows user has, for example, 2 variable named "PATH" - one applied for all users and second one - applied to current user only and could be customized by any user without admin privileges. So, summary `PATH` is result of concatenation of these two variables (system-wide first, user-wide second). – Alex G.P. Jun 15 '15 at 09:21
  • So you want to know which environment varialbe has been set by the user? –  Jun 15 '15 at 09:21
  • @Tichodroma right. What is system-wide `PATH` and what is user-specific one. – Alex G.P. Jun 15 '15 at 09:22

2 Answers2

3

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.

Community
  • 1
  • 1
Andrey
  • 59,039
  • 12
  • 119
  • 163
-2

you can get all the variables by

import os print(os.environ)

this will return you the dictionary type of output with keys as enviornment variables and values as the values of the enviornment variables.

to get the current username:

print(os.environ['USERNAME'])

Look for yourself what you want in the dictionary.

rahul rachh
  • 99
  • 1
  • 12