Python for Lazarus – Embedded Python SetupThis 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 PythonForLazarus1. Open Lazarus
2. Package → Online Package Manager
3. Search for PythonForLazarus
4. Install and restart Lazarus if requested
2. Create a new project1. Create a new Application project
2. Add these components to the main form:
- TPythonEngine
- TPythonInputOutput
3. Example names:
- PythonEngine1
- PythonInputOutput1
3. Download embedded Python1. Download Windows embeddable Python (64-bit)
Example:
https://www.python.org/ftp/python/3.14.2/python-3.14.2-embed-amd64.zip2. Extract it into your project folder
3. Rename the folder to:
your_project_folder\python
4. Check that the Python DLL exists:
your_project_folder\python\python314.dll
4. Enable site moduleOpen this file:
python314._pth
Change:
#import site
To:
import site
This step is required for pip and external libraries.
5. Install pip1. 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:
python get-pip.py
A Lib folder will be created automatically.
6. Extract Python standard library1. Find python314.zip inside the python folder
2. Extract it to:
your_project_folder\python\Lib
7. Install pandasFrom the python folder, run:
python -m pip install pandas
Path warnings can be ignored.
8. Add required Pascal unitAdd proc_py.pas from PythonForLazarus demo sources:
Project → Add Editor File to Project…
9. Component configurationConnect Python output:
PythonEngine1.IO = PythonInputOutput1
For PythonEngine1, uncheck:
- AutoLoad
- UseLastKnownVersion
10. Show Python output inside LazarusTo display Python print() output in a Memo:
procedure TForm1.PythonInputOutput1SendData(Sender: TObject; const Data: ansistring);
begin
memoOutput.Lines.Add(Data);
end;
11. Configure Python paths (AfterInit)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 DLLprocedure TForm1.FormShow(Sender: TObject);
begin
PythonEngine1.DllPath := ExtractFilePath(Application.ExeName) + 'python';
PythonEngine1.DllName := 'python314.dll';
PythonEngine1.LoadDLL;
MaskFPUExceptions(True);
end;
13. Test with pandasPython test code
(Reference:
https://www.w3schools.com/python/pandas/pandas_getting_started.asp)
import pandas
mydataset = {'cars': ["BMW", "Volvo", "Ford"], 'passings': [3, 7, 2]}
myvar = pandas.DataFrame(mydataset)
print(myvar)
Expected output:
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