脚本专栏 
首页 > 脚本专栏 > 浏览文章

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

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )

背景

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

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

代码

py3.5.2

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_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 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_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安装信息加入注册表的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:通过celery异步处理一个查询任务的完整代码
下一篇:Django 自动生成api接口文档教程
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网