Skip to content

First steps

Salvador Díaz Fau edited this page Oct 23, 2026 · 1 revision

Adding Python4Pascal to the search path in Delphi

If you use Delphi, the first thing you need to do in your application is adding the source directory of Python4Pascal to the search path in the the project properties window.

In recent Delphi versions you have to click on the Project menu option and select Options. Then select the Building->Delphi Compiler option in the left panel, select "All configurations" in the Target box, add the full path to the Python4Pascal's sources in the "Search path" option and then click the Save button.

Adding the Python4Pascal requirement in Lazarus

In the case of Lazarus, click on the Project menu and select Project Inspector. Click on the Add button, select New requirement and select Python4Pascal.

Loading the Python library

Your application needs to load the Python libraries before running your first Python script.

Python4Pascal uses a global variable called GlobalP4PEngine to load and initialize Python easily. You'll need to add uP4P.Engine to the uses section of your form.

As you can see in the SimplePythonCodeRunner demo, all you need to do is create GlobalP4PEngine like this: GlobalP4PEngine := TP4PEngine.Create;

If you want to show the output messages from Python then use the GlobalP4PEngine.OnOutputMessage event too. The SimplePythonCodeRunner demo shows the output messages in a TMemo with the TMainForm.GlobalP4PEngine_OnOutputMessage procedure.

After creating the engine you need to load and initialize the Python libraries with these two functions:

  • GlobalP4PEngine.Load
  • GlobalP4PEngine.Initialize

The SimplePythonCodeRunner demo calls those functions and shows the result message in the TMemo.

Calling GlobalP4PEngine.Load without parameters means that Python4Pascal will try to load the Python library installed in the operating system.

Running your first Python script

The easiest way to run your Python scripts is using the TPyRun.SimpleString function. Add uP4P.PyRun to the uses section of your form before using TPyRun.SimpleString.

See how the SimplePythonCodeRunner demo calls that function with the contents of a TMemo.

Destroy the engine

It's also necessary to destroy the engine before closing your application. Use the DestroyGlobalP4PEngine procedure when your application closes.

Clone this wiki locally