Jump to content
Sign in to follow this  
tomye

Python DLL Init question

Recommended Posts

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?

 

1650508419(1).thumb.png.212d414a684ce2d9c63dd9a104548ea6.png

 

Edited by tomye

Share this post


Link to post

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 by shineworld

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×