Delphi 4 Python

Viewed 510

I want to use the Delphi 4 Python components from here https://github.com/pyscripter/python4delphi but I don't want to drop the components on a form, I want everything in code , my code goes like this :

 var
  PythonEngine_netA: TPythonEngine;
  PythonInputOutput_netA: TPythonInputOutput;
begin
  PythonEngine_netA := TPythonEngine.Create(Self);
  PythonInputOutput_netA := TPythonInputOutput.Create(Self);
  try
    ///  configure the components
    PythonEngine_netA.DllName:='python39.dll';
    PythonEngine_netA.IO := PythonInputOutput_netA;
    PythonEngine_netA.UseLastKnownVersion := True;

    PythonInputOutput_netA.OnSendUniData := PythonInputOutput_SendUniData;
    PythonInputOutput_netA.UnicodeIO := True;
    PythonInputOutput_netA.RawOutput := True;

    ///  execute  the script
    PythonEngine_netA.ExecString(UTF8Encode(mmo_pythoncode.text));
  finally
    PythonEngine_netA.free;
    PythonInputOutput_netA.free;
  end;

execution of this code fails, error msg : "Python is not properly initialized", what did I miss to use this code ?

2 Answers

One quick look at PythonEngine.pas (or even better: always search all files for the error message to find out where and why an error is returned) tells me you missed calling PythonEngine_netA.Initialize().

Also note that /Demos describes:

Demo34 Dynamically creating, destroying and recreating PythonEngine. Uses PythonVersions

So please have a look at /Demos/Demo34/Unit1.pas how it is done there with (almost) no components. Or run the whole project in general, preferably in debug mode single stepping thru it be aware which method does what.

You just forgot to load the Dll:

PythonEngine_netA.UseLastKnownVersion:= True;
//PythonEngine_netA.opendll(PYDLL)
PythonEngine_netA.LoadDll;
PythonEngine_netA.IO:= PythonInputOutput_netA;
Related