1

How can you get a list of the environment variables for the current user ("user variables") using Python?

os.environ() returns the system variables, and changing those requires admin access.

I want to have it change the user variables for PATH, as that can be done without any restrictions.

3
  • You want to change the PATH var only for your program or you want to modify the user's PATH forever? Commented Nov 14, 2014 at 13:38
  • Not sure what you mean. What I need is to add a folder to path, and there are 2 options (2 PATH variables): one in "system variables" that requires admin rights to change, and one in "user variables" that doesn't Commented Nov 14, 2014 at 15:16
  • For windows I can recommend a module to set variables through registry. Also has CLI app: github.com/beliaev-maksim/py_setenv this has good control on user/system level and does not have limit in length as setx Commented Feb 12, 2021 at 19:36

3 Answers 3

3

That's wrong. os.environ returns the environment of current process. At this level, there is no notion of user or system variables.

You can of course change any of these environment variables. For PATH just do :

os.environ['PATH'] = new_path

But you are only changing the current process environment. That means that this new environment will be used by current process and all its childs, but will vanish at the end of the process.

There is no portable way to modify the environment of the calling shell

Anyway in windows, you can modify the permanent environment variables with the command setx. For example if you want to set on change the user environment variable FOO to bar, you could do in a python script :

import os
os.system("setx FOO bar")

But this change will only be used by processes started from Windows explorer after the command has been executed. In particular neither the environment of the python script nor the one of the calling cmd.exe if any will be changed.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes it is possible

import subprocess
raw_path = subprocess.run(["powershell", "-Command","[Environment]::GetEnvironmentVariable('Path','User')"], capture_output=True) 
user_path = raw_path.stdout.decode('ascii').split(';') print(user_path)

Comments

0

this will add environment variables to the system as shown enter image description here

import winreg as reg
import ctypes

# Define the registry key and the variable you want to set
key = reg.HKEY_LOCAL_MACHINE
sub_key = r"SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
variable_name = "abdo"
variable_value = "1"

# Open the registry key
with reg.OpenKey(key, sub_key, 0, reg.KEY_SET_VALUE) as registry_key:
    # Set the new environment variable
    reg.SetValueEx(registry_key, variable_name, 0, reg.REG_SZ, 
    variable_value)

# Notify the system that the environment has changed
# This causes the system to reload the environment variables
ctypes.windll.user32.SendMessageW(0xFFFF, 0x1A, 0, 0)

print(f"Environment variable '{variable_name}' set to 
'{variable_value}'.")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.