python 版本切换脚本(切记根据自己的需求更改,出错自己负责)

# -*- coding: utf-8 -*-
author = 'nathan'


import os
import re
import getpass
import sys
import time
import _winreg
import ctypes

import win32api
import win32gui, win32con 
def version_change():
    #注册表去查看自己所需要修改的位置,_winreg.KEY_ALL_ACCESS是SAM属性,win7,win8需先在注册表中开放用户权限
    hKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, r'SYSTEM\ControlSet001\Control\Session Manager\Environment',0,_winreg.KEY_ALL_ACCESS) 
    # get the current value
    value, typ = _winreg.QueryValueEx (hKey, 'path')
    #print value
    try:
        value, typ = _winreg.QueryValueEx (hKey, 'current_py_version')
    except:
        print r"新建系统环境变量current_py_version"
        #newKey = _winreg.CreateKey(hKey,"current_py_version")
        #_winreg.SetValue(newKey,"ValueName",0,"ValueContent")
        _winreg.SetValueEx(hKey, "current_py_version", 0, typ, ''.join('C:\Python27') )


    print'---更改current_py_version中----'


    value, typ = _winreg.QueryValueEx (hKey, 'current_py_version')
    print "原本为:",value
    if (value=='C:\Python27'):
        _winreg.SetValueEx(hKey, "current_py_version", 0, typ, ''.join(r'C:\Users\nathan\AppData\Local\Programs\Python\Python35') )
    else:
        _winreg.SetValueEx(hKey, "current_py_version", 0, typ, ''.join(r'C:\Python27') )    
    value, typ = _winreg.QueryValueEx (hKey, 'current_py_version')
    print "现版本为",value
    rc, dwReturnValue = win32gui.SendMessageTimeout(win32con.HWND_BROADCAST, win32con.WM_SETTINGCHANGE, 0, "Environment", win32con.SMTO_ABORTIFHUNG, 5000)
version_change()
path = os.environ['path']

print path


解决了注册表更新的问题,不过需要使用到win32


猜你喜欢

转载自blog.csdn.net/nathan1025/article/details/73649329
今日推荐