pyscripter/python4delphi

Run the .py file immediately with parameters

Uefi1 opened this issue · 8 comments

Uefi1 commented

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

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

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

was created to ask questions

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

Uefi1 commented

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

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

I'm getting errors for some reason

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

Invalid typecast

Uefi1 commented

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

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