首页 > 开发 > Python > 正文

将python安装信息加入注册表的示例

2024-09-09 19:02:34
字体:
来源:转载
供稿:网友

背景

重装系统,发现之前装在E盘的python可以直接使用,就只是将python的安装目录加入到环境变量中,也一直没有管它,今天跟天软交互的时候发现一直不成功,猜测可能是没有注册表信息。

从网上找的一段代码,直接复制运行即可,留存备查。

代码

py3.5.2

import sysfrom winreg import *# tweak as necessaryversion = sys.version[:3]installpath = sys.prefixregpath = "SOFTWARE//Python//Pythoncore//%s//" % (version)installkey = "InstallPath"pythonkey = "PythonPath"pythonpath = "%s;%s//Lib//;%s//DLLs//" % (  installpath, installpath, installpath)def RegisterPy():  try:    reg = OpenKey(HKEY_CURRENT_USER, regpath)  except EnvironmentError as e:    try:      reg = CreateKey(HKEY_CURRENT_USER, regpath)      SetValue(reg, installkey, REG_SZ, installpath)      SetValue(reg, pythonkey, REG_SZ, pythonpath)      CloseKey(reg)    except:      print("*** Unable to register!")      return    print("--- Python", version, "is now registered!")    return  if (QueryValue(reg, installkey) == installpath and    QueryValue(reg, pythonkey) == pythonpath):    CloseKey(reg)    print("=== Python", version, "is already registered!")    return  CloseKey(reg)  print("*** Unable to register!")  print("*** You probably have another Python installation!")if __name__ == "__main__":  RegisterPy()

py2.*版本

'''解决注册问题,pywin32安装存在问题'''import sysfrom _winreg import *# tweak as necessaryversion = sys.version[:3]installpath = sys.prefixregpath = "SOFTWARE//Python//Pythoncore//%s//" % (version)installkey = "InstallPath"pythonkey = "PythonPath"pythonpath = "%s;%s//Lib//;%s//DLLs//" % (  installpath, installpath, installpath)def RegisterPy():  try:    reg = OpenKey(HKEY_CURRENT_USER, regpath)  except EnvironmentError as e:    try:      reg = CreateKey(HKEY_CURRENT_USER, regpath)      SetValue(reg, installkey, REG_SZ, installpath)      SetValue(reg, pythonkey, REG_SZ, pythonpath)      CloseKey(reg)    except:      print "*** Unable to register!"      return    print "--- Python", version, "is now registered!"    return  if (QueryValue(reg, installkey) == installpath and    QueryValue(reg, pythonkey) == pythonpath):    CloseKey(reg)    print "=== Python", version, "is already registered!"    return  CloseKey(reg)  print "*** Unable to register!"  print "*** You probably have another Python installation!"if __name__ == "__main__":  RegisterPy()

以上这篇将python安装信息加入注册表的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网之家。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表