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:

os.unsetenv
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.

Thanks.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.