Recent

Author Topic: Running Pyrhon Script  (Read 1291 times)

tatamata

  • Hero Member
  • *****
  • Posts: 757
    • ZMSQL - SQL enhanced in-memory database
Running Pyrhon Script
« on: April 14, 2021, 05:17:27 pm »
It is said that TProcess.CommandLine is deprecated
https://lazarus-ccr.sourceforge.io/docs/fcl/process/tprocess.commandline.html

But, how then you are supposed to execute a python program (script) having parameters?
E.g., what would be the proper way to execute
Code: Pascal  [Select][+][-]
  1. python3 myscript.py --param1="value1" --param2="value2"

Warfley

  • Sr. Member
  • ****
  • Posts: 449
Re: Running Pyrhon Script
« Reply #1 on: April 14, 2021, 05:23:51 pm »
Quote
it has been superseded by the properties TProcess.Executable and TProcess.Parameters, which should be used instead of CommandLine.
So how about:
Code: Pascal  [Select][+][-]
  1. MyProcess.Executable := 'python3';
  2. MyProcess.Parameters.Add('myscript.py');
  3. MyProcess.Parameters.Add('--param1=value1');
  4. MyProcess.Parameters.Add('--param2=value2');

Note that I left out the "" because these are just for the parser to make them one parameter instead of splitting them, but as we use a parameter list directly, they are not needed

tatamata

  • Hero Member
  • *****
  • Posts: 757
    • ZMSQL - SQL enhanced in-memory database
Re: Running Pyrhon Script
« Reply #2 on: April 14, 2021, 07:19:59 pm »
Hi, thnx, yes this work on Linux, even if multiple words with spaces are passed as parameter.
My questions:
1. does it work on Windows as well (I mean, for multiword parameters, with spaces), or quotes must be provided?
2. it does not work if parameter values has quoted words inside parameter value, for example if you want to pass exact phrase and unquoted words together as parameter (example is a mixed Google search phrase) - so, how to deal with quotes inside parameter value?

lucamar

  • Hero Member
  • *****
  • Posts: 3882
Re: Running Pyrhon Script
« Reply #3 on: April 14, 2021, 07:40:13 pm »
1. does it work on Windows as well (I mean, for multiword parameters, with spaces), or quotes must be provided?

It should. The point is that one uses quotes to help the command interpreter build the parameters array correctly; since TProcess can be considered (kind of) a "command interpreter" and you pass to it a ready made "array" it doesn't need it.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

tatamata

  • Hero Member
  • *****
  • Posts: 757
    • ZMSQL - SQL enhanced in-memory database
Re: Running Pyrhon Script
« Reply #4 on: April 14, 2021, 09:50:51 pm »
Ok, but what about passing parameters having quotes inside (such as Google search phrases), how to do it?

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9280
  • FPC developer.
Re: Running Pyrhon Script
« Reply #5 on: April 14, 2021, 10:15:00 pm »
Ok, but what about passing parameters having quotes inside (such as Google search phrases), how to do it?

As said, for Parameters.Add(), quotes don't matter. The running program will get the value as is.

tatamata

  • Hero Member
  • *****
  • Posts: 757
    • ZMSQL - SQL enhanced in-memory database
Re: Running Pyrhon Script
« Reply #6 on: April 14, 2021, 10:21:21 pm »
Ok, but what about passing parameters having quotes inside (such as Google search phrases), how to do it?

As said, for Parameters.Add(), quotes don't matter. The running program will get the value as is.
I am sorry, bu this is not the case - if string contains double quotes inside the string (not the whole string quoted), it will raise an error.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9280
  • FPC developer.
Re: Running Pyrhon Script
« Reply #7 on: April 14, 2021, 11:02:24 pm »
Ok, but what about passing parameters having quotes inside (such as Google search phrases), how to do it?

As said, for Parameters.Add(), quotes don't matter. The running program will get the value as is.
I am sorry, bu this is not the case - if string contains double quotes inside the string (not the whole string quoted), it will raise an error.

Example, and who raises the error? There might also be platform constraints. We can't of course pass anything the underlying OS doesn't support.
« Last Edit: April 14, 2021, 11:04:39 pm by marcov »

tatamata

  • Hero Member
  • *****
  • Posts: 757
    • ZMSQL - SQL enhanced in-memory database
Re: Running Pyrhon Script
« Reply #8 on: April 14, 2021, 11:55:39 pm »
So, I cannot give you the python program, but the point is that one of the parameters is search phrase for Google search. Value might be, for example
Code: Pascal  [Select][+][-]
  1. car dealer dealership sale sales manager
and this is passed OK
But if search phrase is
Code: Pascal  [Select][+][-]
  1. "car dealer" dealership sale sales manager
python program raises error of not recognized parameters...
I just tested on both Linux and Win10, it is the same.
« Last Edit: April 14, 2021, 11:58:08 pm by tatamata »

engkin

  • Hero Member
  • *****
  • Posts: 2701
Re: Running Pyrhon Script
« Reply #9 on: April 15, 2021, 03:49:47 am »
I don't known about Linux, but probably the same.

When your parameter has a quotation mark, escape it with a backslash, AND surround with quotation marks:
Code: Pascal  [Select][+][-]
  1. process.parameters.add('"--param1=\"car dealer\" dealership sale sales manager"');
  2. process.parameters.add('--param2=car dealer dealership sale sales manager');

As for the second parameter that does not include quotation marks, it gets enclosed in quotation marks automatically if there is a space. You could add them yourself if you want, but not needed.
« Last Edit: April 15, 2021, 03:59:03 am by engkin »

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 510
  • Professional amateur ;-P
Re: Running Pyrhon Script
« Reply #10 on: April 15, 2021, 08:26:32 am »
Hey all,

You can also use a combination of double and single quotes if you want to avoid the escaping:
Code: Pascal  [Select][+][-]
  1. process.parameters.add('''--param1="car dealer" dealership sale sales manager''');
OR
Code: Pascal  [Select][+][-]
  1. process.parameters.add('"--param1=''car dealer'' dealership sale sales manager"');

Hope that helps.

Cheers,
Gus
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1441
  • Former Delphi 1-7, 10.2 user
Re: Running Pyrhon Script
« Reply #11 on: April 15, 2021, 08:46:22 am »
@Gus - I dunno.

Results of your 1 + 2 options:

Quote
'--param1="car dealer" dealership sale sales manager'
"--param1='car dealer' dealership sale sales manager"

cf:

Quote
--param1=\"car dealer\" dealership sale sales manager
Lazarus 2.1 r65061 FPC 3.3.1 r49223 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.1 r65070 FPC 3.3.1 r49223 macOS 11.2.3 aarch64 Xcode 12.4
Lazarus 2.1 r61574 FPC 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 FPC 3.2.0 Win10 Parallels VM

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 963
Re: Running Pyrhon Script
« Reply #12 on: April 15, 2021, 09:03:43 am »
hello,
to execute python from lazarus don't forget PythonForLazarus package (present in OPM)  ;)

Friendly, J.P
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9280
  • FPC developer.
Re: Running Pyrhon Script
« Reply #13 on: April 15, 2021, 09:18:24 am »
I can image this leads to problems on Windows, where arguments are rearranged to a commandline, before being passed to the OS.

On Linux to my best knowledge it should work, and to my best knowledge I have done this in the past.

But the other side might not be able to handle it. Best would be to check what is passed with strace.

tatamata

  • Hero Member
  • *****
  • Posts: 757
    • ZMSQL - SQL enhanced in-memory database
Re: Running Pyrhon Script
« Reply #14 on: April 15, 2021, 09:20:14 am »
Hm, I am confused, I tried this way, it works for Linux, but not working for Windows.  For Linux it seems that it must not be enclosed by quotes manually. For Windows, I have no idea what to do?
Code: Pascal  [Select][+][-]
  1. vSearchPhrase:=Memo_SearchPhrase.Text;
  2. //When search has a quotation mark, escape it with a backslash, AND surround it with quotation marks.
  3. if ContainsText(vSearchPhrase, '"') then begin
  4.    UTF8StringReplace(vSearchPhrase, '"', '\"',[rfReplaceAll,rfIgnoreCase],'');
  5.    {$IFDEF MSWINDOWS}
  6.    //vSearchPhrase:='"' + '--search_phrase=' + vSearchPhrase + '"';  //This does not work!
  7.    //vSearchPhrase:='--search_phrase=' + '"' + vSearchPhrase + '"';   //This does not works!
  8.    vSearchPhrase:='--search_phrase=' + vSearchPhrase; //This does not work as well!
  9.    {$ELSE}
  10.    vSearchPhrase:='--search_phrase=' + vSearchPhrase; //This works!
  11.    {$ENDIF}
  12. end
  13. else begin
  14.   vSearchPhrase:='--search_phrase=' + vSearchPhrase;
  15. end;  
  16.  

 

TinyPortal © 2005-2018