You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.
pyscripter edited this page May 14, 2021
·
7 revisions
How Python for Delphi finds your Python distribution in Windows
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.
Registered versus unregistered vesions
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.
Setting the properties of PythonEngine
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
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.
Using the PythonVersions unit
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) thenorif PythonVersionFromPath(Path, PythonVersion) thenbegin
PythonVersion.AssignTo(PythonEngine)
PythonEngine.LoadDLL
endelse
Here is the TPythonVersion.AssignTo procedure:
procedureTPythonVersion.AssignTo(PythonEngine: TPersistent);
beginif PythonEngine is TPythonEngine thenbegin
TPythonEngine(PythonEngine).UseLastKnownVersion := False;
TPythonEngine(PythonEngine).RegVersion := SysVersion;
TPythonEngine(PythonEngine).DllName := DLLName;
TPythonEngine(PythonEngine).DllPath := DLLPath;
TPythonEngine(PythonEngine).APIVersion := ApiVersion;
if Is_venv thenbegin
TPythonEngine(PythonEngine).VenvPythonExe := PythonExecutable;
TPythonEngine(PythonEngine).SetPythonHome(DLLPath);
endelseifnot 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;