Recent

Author Topic: how to check python  (Read 2316 times)

mpknap

  • Full Member
  • ***
  • Posts: 155
how to check python
« on: September 13, 2020, 08:25:47 am »
how to check with a code if Python is installed on the computer? (Windows)

six1

  • Full Member
  • ***
  • Posts: 117
Re: how to check python
« Reply #1 on: September 13, 2020, 09:24:11 am »
try it with "whereis python" in a TProccess with output in a TStringlist.



marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11458
  • FPC developer.
Re: how to check python
« Reply #2 on: September 13, 2020, 12:54:38 pm »
whereis is no standard windows command afaik

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: how to check python
« Reply #3 on: September 13, 2020, 12:58:02 pm »
Indeed, but Windows has where which is standard.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11458
  • FPC developer.
Re: how to check python
« Reply #4 on: September 13, 2020, 01:38:43 pm »
If I try that I find left overs of WSL installs (0 byte EXE), not a functional python interpreter

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: how to check python
« Reply #5 on: September 13, 2020, 02:13:17 pm »
If it is installed then it will be on path.
Isn't it called by python (stubbed), I am not sure, I don't have it.

Code: Pascal  [Select][+][-]
  1.  
  2.  function  shell(s:pchar):integer ; cdecl external 'msvcrt.dll' name 'system';
  3.  
  4.  begin
  5.  shell('path');
  6.  shell('python');
  7.  
  8.  readln;
  9.  end.
  10.  

Perhaps this is better to see all:
Code: Pascal  [Select][+][-]
  1.  
  2.  function  shell(s:pchar):integer ; cdecl external 'msvcrt.dll' name 'system';
  3.  
  4.  begin
  5.  Writeln('Please wait until "Done"');
  6.  writeln;
  7.   shell('where /r c:\ python | more');
  8.   writeln('Done');
  9.  readln;
  10.  end.
  11.  
« Last Edit: September 13, 2020, 02:58:31 pm by BobDog »

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: how to check python
« Reply #6 on: September 13, 2020, 02:26:11 pm »
If I try that I find left overs of WSL installs (0 byte EXE), not a functional python interpreter
So I conclude that there are two issues. One yours, one OP.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

mpknap

  • Full Member
  • ***
  • Posts: 155
Re: how to check python
« Reply #7 on: September 13, 2020, 06:32:29 pm »
On my laptop work good :
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.   Classes,
  5.   SysUtils,
  6.   Process;
  7.  
  8. var
  9.   AProcess: TProcess;
  10.   AStringList: TStringList;
  11.   s: string;
  12.   // This is where our program starts to run
  13. begin
  14.  
  15.   AProcess := TProcess.Create(nil);
  16.  
  17.   AProcess.Executable := 'python';
  18.   AProcess.Parameters.Add('-V');
  19.  
  20.   AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
  21.  
  22.   AProcess.Execute;
  23.  
  24.   AStringList := TStringList.Create;
  25.   AStringList.LoadFromStream(AProcess.Output);
  26.   writeln(AStringList.Text);
  27.  
  28.   if copy(AStringList.Text, 1, 6) = 'Python' then
  29.     writeln('ok')
  30.   else
  31.     writeln('No Python');
  32.   readln;
  33.   AStringList.Free;
  34.  
  35.   AProcess.Free;
  36. end.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11458
  • FPC developer.
Re: how to check python
« Reply #8 on: September 13, 2020, 06:45:38 pm »
I think that (checking python output) is better than just checking for binary existence.

But it is a bit verbose, my testcode was:

Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2.  
  3. uses process;
  4.  
  5. const res : array [boolean] of string = ('No Python','ok');
  6.  
  7. var s : string;
  8.  
  9. begin
  10.   writeln(res[runcommand('python.exe',['-V'],s)]);
  11. end.

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: how to check python
« Reply #9 on: September 14, 2020, 04:40:05 pm »
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2.  
  3. uses process;
  4.  
  5. const res : array [boolean] of string = ('No Python','ok');
  6.  
  7. var s : string;
  8.  
  9. begin
  10.   writeln(res[runcommand('python.exe',['-V'],s)]);
  11. end.
This might not give the desired/expected output because of Window$ caveats.
M$ hijacked this command: if you execute this command on a Win PC that does not even have Python installed, it opens some sort of marketplace page.
If you run the command after having installed Python, it still might give you the unexpected marketplace pop up, because of the search path.

When you (manually) move the Python path to the beginning of the environment path string, it will work.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: how to check python
« Reply #10 on: September 15, 2020, 08:10:13 am »
If it's Windows, why not just read the Registry (HKLM/Software.....) and then a FileExists to make sure?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

mpknap

  • Full Member
  • ***
  • Posts: 155
Re: how to check python
« Reply #11 on: September 15, 2020, 09:02:35 pm »
If it's Windows, why not just read the Registry (HKLM/Software.....) and then a FileExists to make sure?

Code please  :)

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: how to check python
« Reply #12 on: September 16, 2020, 08:00:02 am »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

AlexTP

  • Hero Member
  • *****
  • Posts: 2406
    • UVviewsoft
Re: how to check python
« Reply #13 on: September 16, 2020, 10:46:31 am »
Registry keys and the code to read registry, you can find in repo (seach for "registry")
https://github.com/pyscripter/python4delphi

 

TinyPortal © 2005-2018