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.


7 Years
Discussion Span
Last Post by GDICommander

This may help: http://stackoverflow.com/questions/488449/can-a-python-script-persistently-change-a-windows-environment-variable-elegantl

The answer with the use of the setx.exe program looks interesting. If windows has a program to modify environment variables from the command line, a python script can easily call this program with the subprocess module.

Another direction is to use ironpython instead of python, and .net tools.

Edited by Gribouillis: n/a


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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.