将python安装信息加入注册表的示例
作者:潜水的飞鱼baby 时间:2023-02-24 18:15:11
背景
重装系统,发现之前装在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()
来源:https://blog.csdn.net/u011702002/article/details/79624256
标签:python,安装,注册表
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pandas 实现将NaN转换为None
2023-01-23 21:23:09
![](https://img.aspxhome.com/file/2023/8/131238_0s.png)
python实现BackPropagation算法
2022-03-26 12:06:39
Python进阶_关于命名空间与作用域(详解)
2022-03-17 17:29:02
一篇文章带你深入了解Mysql触发器
2024-01-14 15:42:35
vue新玩法VueUse工具库具体用法@vueuse/core详解
2023-07-02 16:55:44
SQL Server数据库日志清除的两个方法
2009-01-08 13:44:00
人工智能学习PyTorch实现CNN卷积层及nn.Module类示例分析
2021-10-11 21:14:42
![](https://img.aspxhome.com/file/2023/9/133239_0s.png)
php foreach循环中使用引用的问题
2023-11-17 17:22:26
uniapp实现人脸识别功能详细示例
2024-04-16 08:59:57
![](https://img.aspxhome.com/file/2023/7/136727_0s.jpg)
Python条件语句的使用
2023-01-04 04:03:36
![](https://img.aspxhome.com/file/2023/4/101284_0s.png)
python3.8下载及安装步骤详解
2023-11-19 18:47:02
![](https://img.aspxhome.com/file/2023/5/123835_0s.png)
python使用xmlrpclib模块实现对百度google的ping功能
2023-05-06 19:02:21
mysql数据校验过程中的字符集问题处理
2024-01-28 15:16:16
![](https://img.aspxhome.com/file/2023/2/112332_0s.png)
python将天气预报可视化
2021-01-19 01:37:46
![](https://img.aspxhome.com/file/2023/5/110495_0s.jpg)
mysql show操作简单示例
2024-01-24 14:42:52
详解Python中的array数组模块相关使用
2021-03-05 04:04:19
Python实现将16进制字符串转化为ascii字符的方法分析
2023-11-17 12:26:25
成为一个顶级设计师的第一准则
2008-04-18 10:29:00
![](https://img.aspxhome.com/file/UploadPic/20084/18/2008418103351248s.jpg)
python 引用传递和值传递详解(实参,形参)
2023-10-25 15:57:44
![](https://img.aspxhome.com/file/2023/7/127267_0s.jpg)
xml css htc 的综合运用
2008-10-10 11:41:00