将python安装信息加入注册表的示例
2019-11-22 12:08
1646 查看
背景
重装系统,发现之前装在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安装信息加入注册表的示例就是小编分享给大家的全部内容了,希望能给大家一个参考
您可能感兴趣的文章:
相关文章推荐
- 将python安装信息加入注册表
- python通过SNMP协议收集服务器监控信息(安装、配置、示例)
- C++访问注册表获取已安装软件信息列表示例代码
- C++访问注册表获取已安装软件信息列表示例代码
- python从注册表中获取win7屏保相关信息和已安装程序
- python登录并爬取淘宝信息代码示例
- python 注册表信息
- c#实例化继承类,必须对被继承类的程序集做引用 .net core Redis分布式缓存客户端实现逻辑分析及示例demo 数据库笔记之索引和事务 centos 7下安装python 3.6笔记 你大波哥~ C#开源框架(转载) JSON C# Class Generator ---由json字符串生成C#实体类的工具
- Python实现socket信息发送和监听的简单代码示例
- Python安装插件实现调用城市天气信息
- Python的scrapy框架爬虫项目中加入邮箱通知(爬虫启动关闭等信息以邮件的方式发送到邮箱)
- 以下示例显示返回当前安装的版本信息。
- Python二维码生成库qrcode安装和使用示例
- 【机器学习】决策树 python(示例+代码)Windows下载及安装 Graphviz
- docker示例--下载ubuntu镜像安装python
- [python] 使用urllib从页面上采集信息示例
- Python实现自动添加脚本头信息的示例代码
- [python]书籍信息爬虫示例
- 安装pywin32时,出现找不到python27注册信息的解决办法
- 获取软件在注册表的安装信息