Difference between revisions of "Using Python in Lazarus on Windows/Linux"

From Free Pascal wiki
Jump to navigationJump to search
Line 19: Line 19:
 
[[File:Python app Windows.png]]
 
[[File:Python app Windows.png]]
  
= Make code =
+
= Code =
 
== P4D ==
 
== P4D ==
  
Package "P4Dlaz.dpk": delete all refs to units "...Delphi..."; remove usage of units in code. Make modifications to PythonEngine.pas, see lines with "//AT". Compile and use package. You must see "Python" tab appeared in component pallette.
+
Package "p4dlaz.lpk": delete all refs to units "...Delphi...".
 +
(Maybe units aren't needed for Lazarus apps.)
 +
Make modifications to PythonEngine.pas, see lines with "//AT". Compile and use package. You must see "Python" tab appeared in component pallette.
  
 
== Program ==
 
== Program ==
 +
 +
Put on form: TPythonEngine, TPythonInputOutput,
 +
set such props of PythonEngine:
 +
 +
* AutoLoad=False
 +
* DllName empty
 +
* DllPath empty
 +
* FatalAbort=False
 +
* InitScript="import sys; print('Python', sys.version)"
 +
* IO=PythonInputOutput1
 +
* PyFlags=[pfIgnoreEnvironmentFlag]
 +
* UseLastKnownVersion=False
 +
 +
Write event handlers:
 +
 +
* PythonEngine.AfterInit: must add paths to "sys.path". Must do only for Windows, for portable Python, don't do for Linux. I add paths: dir+'DLLs', dir+'python33.zip', dir+'Py' (dir is ExtractFilePath(Application.ExeName)).
 +
 +
    procedure TfmMain.PythonEngineAfterInit(Sender: TObject);
 +
    var
 +
      SDir, S1, S2, S3: string;
 +
    begin
 +
      {$ifdef windows}
 +
      SDir:= ExtractFilePath(Application.ExeName);
 +
      S1:= SDir + 'DLLs';
 +
      S2:= SDir + 'python33.zip';
 +
      S3:= SDir + 'Py';
 +
      Py_SetSysPath([S1, S2, S3]);
 +
      {$endif}
 +
    end;

Revision as of 22:42, 8 February 2015

Intro

I need to make app, cross platform, Win/Linux, which embeds Python engine. I tried Python4Delphi, but it doesn't compile and work on Linux x64. Same app must run on Win and Linux, must use portable Python 3 on Win and system Python 3 on Linux.

Result

Links

Work on Ubuntu 14.04 x64

Python app Linux.png

Work on Windows7 x64

Python app Windows.png

Code

P4D

Package "p4dlaz.lpk": delete all refs to units "...Delphi...". (Maybe units aren't needed for Lazarus apps.) Make modifications to PythonEngine.pas, see lines with "//AT". Compile and use package. You must see "Python" tab appeared in component pallette.

Program

Put on form: TPythonEngine, TPythonInputOutput, set such props of PythonEngine:

  • AutoLoad=False
  • DllName empty
  • DllPath empty
  • FatalAbort=False
  • InitScript="import sys; print('Python', sys.version)"
  • IO=PythonInputOutput1
  • PyFlags=[pfIgnoreEnvironmentFlag]
  • UseLastKnownVersion=False

Write event handlers:

  • PythonEngine.AfterInit: must add paths to "sys.path". Must do only for Windows, for portable Python, don't do for Linux. I add paths: dir+'DLLs', dir+'python33.zip', dir+'Py' (dir is ExtractFilePath(Application.ExeName)).
   procedure TfmMain.PythonEngineAfterInit(Sender: TObject);
   var
     SDir, S1, S2, S3: string;
   begin
     {$ifdef windows}
     SDir:= ExtractFilePath(Application.ExeName);
     S1:= SDir + 'DLLs';
     S2:= SDir + 'python33.zip';
     S3:= SDir + 'Py';
     Py_SetSysPath([S1, S2, S3]);
     {$endif}
   end;