python管理windows的环境变量,python环境变量,下面的代码可以用来修改w


下面的代码可以用来修改windows的用户或者系统变量,python2和python3都适用:

import sysfrom subprocess import check_callif sys.hexversion > 0x03000000:    import winregelse:    import _winreg as winregclass Win32Environment:    """Utility class to get/set windows environment variable"""    def __init__(self, scope):        assert scope in ('user', 'system')        self.scope = scope        if scope == 'user':            self.root = winreg.HKEY_CURRENT_USER            self.subkey = 'Environment'        else:            self.root = winreg.HKEY_LOCAL_MACHINE            self.subkey = r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'    def getenv(self, name):        key = winreg.OpenKey(self.root, self.subkey, 0, winreg.KEY_READ)        try:            value, _ = winreg.QueryValueEx(key, name)        except WindowsError:            value = ''        return value    def setenv(self, name, value):        # Note: for 'system' scope, you must run this as Administrator        key = winreg.OpenKey(self.root, self.subkey, 0, winreg.KEY_ALL_ACCESS)        winreg.SetValueEx(key, name, 0, winreg.REG_EXPAND_SZ, value)        winreg.CloseKey(key)        # For some strange reason, calling SendMessage from the current process        # doesn't propagate environment changes at all.        # TODO: handle CalledProcessError (for assert)        check_call('''\"%s" -c "import win32api, win32con; assert win32api.SendMessage(win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, 'Environment')"''' % sys.executable)

使用方法如下:

    >>> e1 = Win32Environment(scope="system")    >>> print(e.getenv('PATH'))    >>> e2 = Win32Environment(scope="user")    >>> e2.setenv('PYTHONPATH', os.path.expanduser(r'~\mymodules'))

上面的程序将显示PATH变量的值,并设置用户的PATHONPATH变量的值。

评论关闭