tomye 1 Posted April 21, 2022 (edited) my environment is win7 , python version is 3.8 (x64) and D11 the installation of P4D was quite well but run the first DEMO (demo1), nothing happened and i tried to add a code on engine beforeload PythonEngine1.SetPythonHome(PythonEngine1.DllPath); it works, someone knows why the P4D cannot load the python automatically? or the OS must be win10 above? Edited April 21, 2022 by tomye Share this post Link to post
shineworld 73 Posted April 21, 2022 (edited) There are many ways to "define" what Pythonxxx.DLL will be used by PythonEngine used in the Delphi application or in a PYD extension module made with Delphi. You can set "MyEngine.UseLaskKnownVersion := True" so the Engine search for 3.10, then 3.9, ... till the oldest version is supported. You can also "fix" a version leaving it to PythonEngine to search for the path. This is done thanks to fact that Python "stores" any installed Python version path in Windows Registry. {** * TAKE CARE * ========= * PYD modules need to be build for a specific Python version using below defines. * The defines MUST be mutually exclusive. * If not defined the "Use Last Known Version" will be used. * **} {$DEFINE USE_PYTHON_39} {.DEFINE USE_PYTHON_310} {$IF Defined(USE_PYTHON_39) and Defined(USE_PYTHON_310)} {$Message Fatal 'Only one Python version can be set.'} {$IFEND} { module import functions } function PyInit_cnc_vision_ext: PPyObject; begin Result := nil; try ExtensionManager.FEngine := TPythonEngine.Create(nil); ExtensionManager.FEngine.AutoFinalize := False; {$IF Defined(USE_PYTHON_39)} ExtensionManager.FEngine.DLLName := 'python39.dll'; ExtensionManager.FEngine.UseLastKnownVersion := False; ExtensionManager.FEngine.OpenDll(ExtensionManager.FEngine.DLLName); {$ELSEIF Defined(USE_PYTHON_310)} ExtensionManager.FEngine.DLLName := 'python310.dll'; ExtensionManager.FEngine.UseLastKnownVersion := False; ExtensionManager.FEngine.OpenDll(ExtensionManager.FEngine.DLLName); {$ELSE} ExtensionManager.FEngine.UseLastKnownVersion := True; ExtensionManager.FEngine.OpenDll(''); {$IFEND} ExtensionManager.FModule := TPythonModule.Create(nil); ExtensionManager.FModule.Engine := ExtensionManager.FEngine; ExtensionManager.FModule.ModuleName := 'cnc_vision_ext'; ExtensionManager.FWrapper := TPyDelphiWrapper.Create(nil); ExtensionManager.FWrapper.Engine := ExtensionManager.FEngine; ExtensionManager.FWrapper.Module := ExtensionManager.FModule; ExtensionManager.FModule.Initialize; ExtensionManager.FWrapper.OnInitialization := ExtensionManager.WrapperInitializationEvent; ExtensionManager.FWrapper.Initialize; Result := ExtensionManager.FModule.Module; except end; end; This is a part of my extension module in which I can define if the module is built for 3.9, 3.10, or looking for the last known version. OpenDLL internally searches in Windows Registry where the requested python engine path is. Take a look at file: PythonVersions.pas GetRegisteredPythonVersion() function there is also a good description of system. https://github.com/pyscripter/python4delphi/wiki/FindingPython I don't know how works with Anaconda envs or embedded python distros. Edited April 21, 2022 by shineworld Share this post Link to post