Hello, DaniWeb community!

I want to know how I can delete environment variables in Python so that I can no longer see them outside of the execution of the Python script (on Windows, in "Advanced System Settings/Environment Variables...")

This is what I tried:

deleting entries in os.environ, but it only affects the script environment...

I'm thinking of playing in the registry or modified a file called AUTOEXEC.BAT on Windows, but I hope that something in Python exists that can prevent me from using these "hardcore" methods. Remember, I need the deletion of environment variables to take effect right now.


Hello, Gribouillis.

setx.exe cannot remove environment variables from a Windows system, it can only add or modify environment variables. So, I used manipulations in the registry, like I was suggesting in my first post, to remove an environment variable.

For your information, the environment variables are at this place in the registry:

[HKEY_CURRENT_USER\Environment] #for user environment variables
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment] #for system variables

I used the _winreg module in pywin to edit the registry.

Thanks for your help.

This question has already been answered. Start a new discussion instead.