import sys
from winreg import *
# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix
regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
installpath, installpath, installpath)
def RegisterPy():
try:
reg = OpenKey(HKEY_LOCAL_MACHINE, regpath)
except EnvironmentError:
try:
reg = CreateKey(HKEY_LOCAL_MACHINE, regpath)
SetValue(reg, installkey, REG_SZ, installpath)
SetValue(reg, pythonkey, REG_SZ, pythonpath)
CloseKey(reg)
except:
print("Unable to register! %s " % (pythonpath))
return
print ("--- Python"+ version+"is now registered!")
return
print ("--- Python"+ version+"is now registered!")
RegisterPy()
保存为*.py 然后用管理员权限执行,跟python2.x相比有点区别,两个方面
1 导入包为 winreg python 2.x 为 _winreg
2 print 的差别