Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork320
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 fromwww.python.org or theAnaconda 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 thelatest registered Python version installed
- Set UseLastKnownVerion property to True
- If you want aspecific 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 specificunregistered 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: TPythonVersionif GetRegisteredPythonVersion(SysVersion, PythonVersion)thenorif PythonVersionFromPath(Path, PythonVersion)thenbegin PythonVersion.AssignTo(PythonEngine) PythonEngine.LoadDLLendelse
Here is the TPythonVersion.AssignTo procedure:
procedureTPythonVersion.AssignTo(PythonEngine: TPersistent);beginif PythonEngineis TPythonEnginethenbegin TPythonEngine(PythonEngine).UseLastKnownVersion := False; TPythonEngine(PythonEngine).RegVersion := SysVersion; TPythonEngine(PythonEngine).DllName := DLLName; TPythonEngine(PythonEngine).DllPath := DLLPath; TPythonEngine(PythonEngine).APIVersion := ApiVersion;if Is_venvthenbegin TPythonEngine(PythonEngine).VenvPythonExe := PythonExecutable; TPythonEngine(PythonEngine).SetPythonHome(DLLPath);endelseifnot IsRegisteredor Is_condathen{ 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