Recent

Author Topic: First PythonForLazarus Demo for Windows - Embedded Python Setup with pip support  (Read 367 times)

fozkan

  • New member
  • *
  • Posts: 8
Python for Lazarus – Embedded Python Setup

This guide explains how to use Python for Lazarus (Python4Lazarus)
with a Windows embeddable Python distribution and run Python code
(including pip and pandas) inside a Lazarus application.

Important: 
This guide is based on the Python version available at the time of writing.
When installing, you may safely use the latest available embeddable Python version.
Only the DLL name and paths must be updated consistently.

The steps below were verified using a working Lazarus demo project.



Assumptions
- Lazarus IDE (Online Package Manager available)
- Windows OS
- Lazarus and Python have the same bitness (64-bit ↔ 64-bit)



1. Install PythonForLazarus

1. Open Lazarus 
2. Package → Online Package Manager 
3. Search for PythonForLazarus 
4. Install and restart Lazarus if requested 



2. Create a new project

1. Create a new Application project 
2. Add these components to the main form:
   - TPythonEngine
   - TPythonInputOutput
3. Example names:
   - PythonEngine1
   - PythonInputOutput1



3. Download embedded Python

1. Download Windows embeddable Python (64-bit) 
   Example: 
   https://www.python.org/ftp/python/3.14.2/python-3.14.2-embed-amd64.zip

2. Extract it into your project folder 
3. Rename the folder to:

Code: [Select]
your_project_folder\python

4. Check that the Python DLL exists:

Code: [Select]
your_project_folder\python\python314.dll



4. Enable site module

Open this file:

Code: [Select]
python314._pth

Change:

Code: [Select]
#import site

To:

Code: [Select]
import site

This step is required for pip and external libraries.



5. Install pip

1. Download get-pip.py into the python folder 
   https://bootstrap.pypa.io/get-pip.py 
2. Open command prompt in the python folder 
3. Run:

Code: [Select]
python get-pip.py

A Lib folder will be created automatically.



6. Extract Python standard library

1. Find python314.zip inside the python folder 
2. Extract it to:

Code: [Select]
your_project_folder\python\Lib



7. Install pandas

From the python folder, run:

Code: [Select]
python -m pip install pandas

Path warnings can be ignored.



8. Add required Pascal unit

Add proc_py.pas from PythonForLazarus demo sources:

Code: [Select]
Project → Add Editor File to Project…



9. Component configuration

Connect Python output:

Code: [Select]
PythonEngine1.IO = PythonInputOutput1

For PythonEngine1, uncheck:
- AutoLoad
- UseLastKnownVersion



10. Show Python output inside Lazarus

To display Python print() output in a Memo:

Code: [Select]
procedure TForm1.PythonInputOutput1SendData(Sender: TObject; const Data: ansistring);
begin
  memoOutput.Lines.Add(Data);
end;



11. Configure Python paths (AfterInit)

Code: [Select]
procedure TForm1.PythonEngine1AfterInit(Sender: TObject);
var
  dir: string;
begin
  dir := ExtractFilePath(Application.ExeName) + cPyFolder;
  Py_SetSysPath(
    [
      dir + '\Lib\site-packages',
      dir + '\Lib',
      dir
    ],
    False
  );
end;

This allows Python to find standard libraries and pip packages.



12. Load Python DLL

Code: [Select]
procedure TForm1.FormShow(Sender: TObject);
begin
  PythonEngine1.DllPath := ExtractFilePath(Application.ExeName) + 'python';
  PythonEngine1.DllName := 'python314.dll';
  PythonEngine1.LoadDLL;

  MaskFPUExceptions(True);
end;



13. Test with pandas

Python test code 
(Reference: https://www.w3schools.com/python/pandas/pandas_getting_started.asp)

Code: [Select]
import pandas

mydataset = {'cars': ["BMW", "Volvo", "Ford"], 'passings': [3, 7, 2]}
myvar = pandas.DataFrame(mydataset)
print(myvar)

Expected output:

Code: [Select]
    cars  passings
0    BMW         3
1  Volvo         7
2   Ford         2



Notes
- Lazarus and Python must have the same bitness 
- Embedded Python does not change the system PATH 
- All Python files stay inside the application folder 
« Last Edit: December 09, 2025, 04:27:16 pm by fozkan »

 

TinyPortal © 2005-2018