pyscripter/python4delphi

How can I output the results of a py script?

Uefi1 opened this issue · 9 comments

Hello, how can I output the results to a string variable of a .py script running in a multi-threaded application ?

function ExecPythonFile(const Filename):string;
var
FGILState: PyGILstate_STATE;
begin
Result := '';
FGILState := PythonEngine.PyGILState_Ensure;
try
PythonEngine.ExecFile(Filename);
except
PythonEngine.IO.Write(Result); //No WORK ((((
end;
PythonEngine.PyGILState_Release(FGILState);
end;

Exec the script containing 'print( )' and use

property IO: TPythonInputOutput read FIO write FIO;               
property RedirectIO: Boolean read FRedirectIO write FRedirectIO default True;   

Exec the script containing 'print( )' and use

Thanks, I figured it out a little but it doesn't work (

function ExecPythonFile(const Filename):string;
var
FGILState: PyGILstate_STATE;
PythonInputOutput:TPythonGUIInputOutput;
begin
Result := '';
PythonInputOutput:=TPythonGUIInputOutput.Create(nil);
PythonEngine.IO:=PythonInputOutput;
Result:=PythonInputOutput.Output.Text;
FGILState := PythonEngine.PyGILState_Ensure;
try
PythonEngine.DoRedirectIO;
PythonEngine.ExecFile(Filename);
except
end;
PythonEngine.PyGILState_Release(FGILState);
PythonInputOutput.Free;
end;

Result:=PythonInputOutput.Output.Text;
PythonEngine.DoRedirectIO

I never used these 2 methods.
what I use:

Screenshot from 2024-03-11 13-24-58

I never used these 2 methods.

Sorry I don't understand your code a bit (

  • I make new object PythonIO
  • I set its event OnSendUniData (Uni - to get UnicodeString in event handler)
  • and I set PythonIO.UnicodeIO to True
  • I attach PythonIO to TPythonEngine.IO property
  • I attach PythonIO to TPythonEngine.IO property

Still, I think that my last option should work, but I don’t understand why it doesn’t work (
Thank you very much for your help, of course, but you have some other language in which you write, so I don’t understand it yet

  • You should not create and destroy TPythonGUIInputOutput every time you execute a script. This does not work. Create once before the python dll is loaded.
  • To output from threads you can use the DelayWrites property
  • Alternatively see the LessSimpleDemo in https://en.delphipraxis.net/topic/5686-python-code-is-running-in-main-thread-on-standard/
  • Use support forum for answers to questions, not the Issue tracker. If you do it again I will blacklist you from this repo.
  • Use support forum for answers to questions, not the Issue tracker. If you do it again I will blacklist you from this repo.

Hello, I think I did everything as you said, the output still doesn’t work (

var
PythonEngine: TPythonEngine;
PythonInputOutput:TPythonGUIInputOutput;

procedure InitPython;
begin
  PythonEngine := TPythonEngine.Create(nil);
  try
    PythonInputOutput:=TPythonGUIInputOutput.Create(nil);
    PythonInputOutput.DelayWrites:=True;
    PythonEngine.InitThreads := True;
    PythonEngine.UseLastKnownVersion := True;
    PythonEngine.AutoLoad := False;
    PythonEngine.AutoFinalize := False;
    PythonEngine.AutoUnload := False;
    PythonEngine.RedirectIO := True;
    PythonEngine.IO:=PythonInputOutput;
    PythonEngine.LoadDll;
    TPythonThread.Py_Begin_Allow_Threads;
  finally
  end;
end;

function ExecPythonFile(const Filename:string):string;
var
FGILState: PyGILstate_STATE;
begin
Result := '';
FGILState := PythonEngine.PyGILState_Ensure;
try
PythonEngine.ExecFile(Filename);
except
end;
PythonEngine.PyGILState_Release(FGILState);
Result:=PythonInputOutput.Output.Text;
end;

Once again, the Issue tracker on GitHub exists for questions, ideas, feedback, tasks, or bugs for work on GitHub.:
https://docs.github.com/en/issues/tracking-your-work-with-issues/about-issues

Why the hell should I register on forums with dubious content for each individual repository on Github, and what if the database is stolen on such a forum?

In general, the Python4Delphi BUG does not want to display data for any parameters or settings or assigned events onsenddata onreceivedata