Recent

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

mpknap

  • Full Member
  • ***
  • Posts: 145
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

  • Jr. Member
  • **
  • Posts: 90
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8719
  • 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: 10445
Re: how to check python
« Reply #3 on: September 13, 2020, 12:58:02 pm »
Indeed, but Windows has where which is standard.
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8719
  • 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

  • Jr. Member
  • **
  • Posts: 50
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: 10445
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.
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

mpknap

  • Full Member
  • ***
  • Posts: 145
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8719
  • 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: 1608
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

  • Sr. Member
  • ****
  • Posts: 392
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 IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

mpknap

  • Full Member
  • ***
  • Posts: 145
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

  • Sr. Member
  • ****
  • Posts: 392
Re: how to check python
« Reply #12 on: September 16, 2020, 08:00:02 am »
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

Alextp

  • Hero Member
  • *****
  • Posts: 1119
    • 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