วิธีตั้งค่าตัวแปรสภาพแวดล้อม windows จาก python

os.system("SETX {0} {1} /M".format("start", "test"))
import winreg

regdir = "Environment-test"
keyname = "Name-test"
keyvalue = "Value-test"

def setRegistry(regdir, keyname, keyvalue):
    with winreg.CreateKey(winreg.HKEY_CURRENT_USER, regdir) as _:
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, regdir, 0, winreg.KEY_WRITE) as writeRegistryDir:
            winreg.SetValueEx(writeRegistryDir, keyname, 0, winreg.REG_SZ, keyvalue)

def getRegistry(regdir, keyname):
    with winreg.OpenKey(winreg.HKEY_CURRENT_USER, regdir) as accessRegistryDir:
        value, _ = winreg.QueryValueEx(accessRegistryDir, keyname)
        return(value)