https://github.com/pyscripter/python4de … dingPython
Python for Delphi 如何在 Windows 中找到您的 Python 发行版
要使用Python for Delphi,您需要下载并安装python。有许多不同的python发行版,最常见的是来自 www.python.org 或用于数据分析的Anaconda发行版。
已注册与未注册
在 Windows 中安装 Python 时,可以选择为所有用户或当前用户注册它。注册涉及向注册表写入有关安装位置、帮助文件的名称和位置等信息。
设置 PythonEngine 的属性
如果要使用已安装的最新注册的 Python 版本
将“使用上次已知版本”属性设置为 True
如果需要特定的注册版本,则需要设置以下属性:
DLLName 例如 python38.dll
注册表版本,例如 3.8
将“使用上一个已知版本”属性设置为 False
如果要使用特定的未注册版本,请设置以下属性
DLLName 例如 python38.dll
注册表版本,例如 3.8
将“使用上一个已知版本”属性设置为 False
将 DLLPath 设置为 DLL 所在的路径
将自动加载属性设置为 False
将以下语句添加到 PythonEngine OnBeforeLoad 事件处理程序中,假设 PythonEngine 是组件的名称
PythonEngine.SetPythonHome(‘your python installation directory’)
将以下语句添加到 FormCreate 处理程序:
PythonEngine.LoadDll;
笔记:
32 位 Delphi 应用程序仅适用于 32 位版本的 Python,64 位 Delphi 应用程序仅适用于 64 位版本的 Python。
Anaconda 发行版要求你调用 SetPythonHome,即使它们已注册。
使用 PythonVersions 单元
PythonVersion 单元可以帮助正确查找和加载您想要的 python 版本。它处理各种复杂性,例如注册/未注册版本,Anaconda发行版和虚拟环境。要使用它,请将 PythonEngine 的 Autoload 属性设置为 False,然后在 FormCreate load python 中,如下所示:
-
var PythonVersion: TPythonVersion
-
-
if GetRegisteredPythonVersion(SysVersion, PythonVersion) then
-
or
-
if PythonVersionFromPath(Path, PythonVersion) then
-
begin
-
PythonVersion.AssignTo(PythonEngine)
-
PythonEngine.LoadDLL
-
end
-
以下是 TPythonVersion.AssignTo 过程:
-
procedure TPythonVersion.AssignTo(PythonEngine: TPersistent);
-
begin
-
if PythonEngine is TPythonEngine then
-
begin
-
TPythonEngine(PythonEngine).UseLastKnownVersion := False;
-
TPythonEngine(PythonEngine).RegVersion := SysVersion;
-
TPythonEngine(PythonEngine).DllName := DLLName;
-
TPythonEngine(PythonEngine).DllPath := DLLPath;
-
TPythonEngine(PythonEngine).APIVersion := ApiVersion;
-
if Is_venv then begin
-
TPythonEngine(PythonEngine).VenvPythonExe := PythonExecutable;
-
TPythonEngine(PythonEngine).SetPythonHome(DLLPath);
-
end else if not IsRegistered or Is_conda then
-
{
-
Not sure why but PythonHome needs to be set even for
-
registered conda distributions
-
Note also that for conda distributions to work properly,
-
you need to add Format(‘%s;%0:s\Library\bin;’, [Version.InstallPath]
-
to your Windows path if it is not there already.
-
}
-
TPythonEngine(PythonEngine).SetPythonHome(InstallPath);
-
end;
-
end;
原文作者: BG6RSH | 查看原帖