-
-
Notifications
You must be signed in to change notification settings - Fork 314
FindingPython
To use Python for Delphi you need to download and install python. There are a number of different python distributions, the most common ones being from www.python.org or the Anaconda distribution used for data analytics.
When you install Python in Windows, you have the option to register it, either for all users or for the current user. Registration involves writing information to the registry about the location of the installation, the name and location of the help file etc.
- If you want to use the latest registered Python version installed
- Set UseLastKnownVerion property to True
- If you want a specific registered version you need to set the following properties:
- DLLName e.g. python38.dll
- RegVersion e.g 3.8
- Set UseLastKnownVerion property to False
- If you want to use a specific unregistered version set the following properties
- DLLName e.g. python38.dll
- RegVersion e.g 3.8
- Set UseLastKnownVerion property to False
- Set the DLLPath to the path where the DLL is located
- Set the AutoLoad property to False
- Add the following statement to the PythonEngine OnBeforeLoad event handler, assuming that PythonEngine is the name of the component
PythonEngine.SetPythonHome('your python installation directory')
- Add the following statement to your FormCreate handler:
PythonEngine.LoadDll;
Notes:
- 32-bit Delphi applications only work with 32-versions of Python and 64-bit Delphi applications only work with 64-bit versions of Python.
- Anaconda distributions require that you call SetPythonHome even if they are registered.
The PythonVersion unit can help to find and load the python version you want correctly. It deals with various complications such as registred/unregistred versions, Anaconda distributions and virtual environments. To use it set the Autoload property of PythonEngine to False and then in your FormCreate load python as follows:
var PythonVersion: TPythonVersion
if GetRegisteredPythonVersion(SysVersion, PythonVersion) then
or
if PythonVersionFromPath(Path, PythonVersion) then
begin
PythonVersion.AssignTo(PythonEngine)
PythonEngine.LoadDLL
end
else
Here is the TPythonVersion.AssignTo procedure:
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;
See also: Masking FPU Exceptions