2

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 2

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

add this line of code , but get now an AV error when calling : PythonEngine_netA.Initialize() command
How about debugging and single stepping execution thru that method?
error happend at procedure TPythonEngine.AssignPyFlags; already added this code with no success :
PythonFlags_netA : TPythonFlags ; begin .. try /// configure the components /// PythonFlags_netA:=[]; ... PythonEngine_netA.PyFlags := PythonFlags_netA;
And where in that method? Py_DebugFlag is a pointer and is set in TPythonInterface.MapDll - that method is called in TPythonInterface.AfterLoad. And since TPythonEngine inherites from TPythonInterface call PythonInputOutput_netA.AfterLoad(). Also edit your question and use code formattings in your comments.
|
1

You just forgot to load the Dll:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.