pyscripter / python4delphi

Free components that wrap up Python into Delphi and Lazarus (FPC)
MIT License
895 stars 307 forks source link

Run the .py file immediately with parameters #461

Closed Uefi1 closed 3 months ago

Uefi1 commented 4 months ago

Hello, please tell me how to correctly run a ready-made .py file via Python4Delphi if the file itself requires launching with some parameters, for example -h -u -i -o for example, I would like to do something like:

function ExecPythonFile(const Filename,Params:string):string;
var
FGILState: PyGILstate_STATE;
begin
Result := '';
FGILState := PythonEngine.PyGILState_Ensure;
try
PythonEngine.ExecFile(Filename, params); //????
finally
PythonEngine.PyGILState_Release(FGILState);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
s:string;
begin
ExecPythonFile(ExtractFilePath(ParamStr(0))+'pyscript\main.py', ' --help');
end;

And usually I run this file like this: python main.py --help

pyscripter commented 3 months ago

Python stores command line parameters in sys.argv (list object). You can manipulate sys.argv before running the file.

And PLEASE stop using the Issue Tracker for questions.

Uefi1 commented 3 months ago

Issue Tracker is on Github and was created to ask questions, what is the problem?

pyscripter commented 3 months ago

was created to ask questions

No. The issue tracker is for reporting bugs. PLEASE don't use it to ask questions.

Uefi1 commented 3 months ago

No. The issue tracker is for reporting bugs. PLEASE don't use it to ask questions.

Sorry, I don't have many questions, just a couple of questions.

Uefi1 commented 3 months ago

It’s a little unclear how it goes like this ?:

function ExecPythonFile(const Filename:string;params:string):string;
var
FGILState: PyGILstate_STATE;
begin
Result := '';
FGILState := PythonEngine.PyGILState_Ensure;
try
PythonEngine.PySys_SetArgv(0, '-help');
PythonEngine.ExecFile(Filename);
finally
PythonEngine.PyGILState_Release(FGILState);
end;
end;
Uefi1 commented 3 months ago

I'm getting errors for some reason

PythonEngine.PySys_SetArgv(0, PPWideChar('-in -o -m'));

Invalid typecast

Uefi1 commented 3 months ago

Will this be right ?

function ExecPythonFile(const Filename:string;params:string):string;
var
args:PPWideChar;
FGILState: PyGILstate_STATE;
begin
Result := '';
args:=@params;
FGILState := PythonEngine.PyGILState_Ensure;
try
PythonEngine.PySys_SetArgv(0, args);
PythonEngine.ExecFile(Filename);
finally
PythonEngine.PyGILState_Release(FGILState);
end;
end;

ExecPythonFile(ExtractFilePath(ParamStr(0))+'pyscript\main.py, '-i -o -threads 10')

Uefi1 commented 3 months ago

No, it doesn’t work, and if only the first parameter one works, the rest are ignored =( I don’t quite understand how to do it