python路径写入注册表,导入三方模块win32

python在安装第三方模块时候,需要将python的路径写入注册表,否则会提示 ‘python version 3.8-32 required,which was not found in the registry.’此时需要查看你的注册表

以下为检查及写入方法。

一、第一步先检查python路径是否已经写入注册表:

如果已经写入路径,用如下步骤即可安装win32成功:

按下键盘的win+R弹出运行框,输入‘regedit’回车弹出注册表编辑器,如下:

 

 

二、再次去执行,安装win32

 

 

 

 

 

 

 三、CMD命令行验证是否安装成功

如下提示安装成功

 

 

 四、如第一步没有写入路径,在pycham执行如下脚本将python路径写入注册表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路径的3.8修改成3.8-32,即可安装win32成功

 

 

 

posted @ 2020-04-06 11:16  安琪儿一直在  阅读(1085)  评论(0编辑  收藏  举报