问题描述
如何为 Inno Setup Compiler 编写安装脚本,以便仅在正确版本不可用时安装 Python?
如何检查特定的 Python 版本,例如Python 3.6.8,32 位,安装了吗?
解决方法
方法
安装检查可以通过使用注册表函数在安装脚本的代码部分完成。 Python 安装程序在不同位置创建注册表项,具体取决于安装类型:
HKEY_CURRENT_USER\Software\Python\PythonCore
HKEY_LOCAL_MACHINE\Software\Python\PythonCore
HKEY_CURRENT_USER\Software\Wow6432Node\Python\PythonCore
HKEY_LOCAL_MACHINE\Software\Wow6432Node\Python\PythonCore
Python 注册表组织的完整结构可以在 PEP 514 中找到。
解决方案
使用以下函数检查是否安装了 Python 3.6.8、32 或 64 位版本。此功能并不通用,因为 Python 注册表组织已随 Python 版本发生变化。请根据您的需求进行调整,如果您找到其他版本的解决方案,请告诉我们。
[Code]
{Check existence of key in registry and check version string.
return true if Python is installed and version is correct}
function checkKey(regpart: integer; key: string; version: string) : Boolean;
var
installedVersion: string;
begin
Result :=
{ Check if key exists }
RegKeyExists(regpart,Key) and
{ try to get version string }
RegQueryStringValue(regpart,key,'Version',installedVersion) and
{ check version string }
(version = installedVersion);
end;
{ Return true if python 3.6.8 bit is not installed }
function python_3_6_8_is_not_installed() : Boolean;
var
Key : string;
Version: string;
begin
{ check registry }
Key := 'Software\Python\PythonCore\3.6-32';
Version := '3.6.8';
Result :=
{ Check all user 32-bit installation}
(not checkKey(HKLM32,Key,Version)) and
{ Check current user 32-bit installation}
(not checkKey(HKCU32,Version)) and
{ Check all user 64-bit installation}
(not checkKey(HKLM64,Version)) and
{ Check current user 64-bit installation}
(not checkKey(HKCU64,Version));
end;