Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run the .py file immediately with parameters #461

Closed
Uefi1 opened this issue Mar 9, 2024 · 8 comments
Closed

Run the .py file immediately with parameters #461

Uefi1 opened this issue Mar 9, 2024 · 8 comments
Labels

Comments

@Uefi1
Copy link

Uefi1 commented Mar 9, 2024

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
Copy link
Owner

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
Copy link
Author

Uefi1 commented Mar 9, 2024

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

@pyscripter
Copy link
Owner

was created to ask questions

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

@Uefi1
Copy link
Author

Uefi1 commented Mar 9, 2024

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
Copy link
Author

Uefi1 commented Mar 9, 2024

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
Copy link
Author

Uefi1 commented Mar 9, 2024

I'm getting errors for some reason

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

Invalid typecast

@Uefi1
Copy link
Author

Uefi1 commented Mar 10, 2024

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
Copy link
Author

Uefi1 commented Mar 10, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants