Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++CodeDelphi

Quickly Build Powerful Python Modules In Delphi Using Python4Delphi Sample App

A Module is a file containing Python definitions and statements similar to the unit file in Delphi. To learn more about Python modules check this tutorial. How to create such Python Modules in Delphi using Python4Delphi components? This post will guide you. You can also use Python4Delphi with C++Builder.

Python4Delphi Demo5 Sample App shows how to create a Module, add a routine to that module, Import the module in a python script, and access the added routine. You can find the Demo5 source on GitHub.

Prerequisites: Download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out this video Getting started with Python4Delphi.

Components used in Python4Delphi Demo5 App:

  • TPythonEngine: A collection of relatively low-level routines for communicating with Python, creating Python types in Delphi, etc. It’s a singleton class.
  • TPythonGUIInputOutput: Inherited from TPythonInputOutput (which works as a console for python outputs) Using this component Output property you can associate the Memo component to show the Output.
  • TPythonModule: It’s inherited from TMethodsContainer class allows creating modules by providing a name. You can use routines AddMethod, AddMethodWithKW to add a method of type PyCFunction. You can create events using the Events property.
  • TMemo: A multiline text editing control, providing text scrolling. The text in the memo control can be edited as a whole or line by line.

You can find the Python4Delphi Demo5 sample project from the extracted GitHub repository ..\Python4Delphi\Demos\Demo05.dproj. Open this project in RAD Studio 10.4.1 and run the application.

Implementation Details:

  • PythonEngine1 component provides the connection to Python or rather the Python API. This project uses Python3.9 which can be seen in TPythonEngine DllName property. It Is assigned with InitScript which import sys module and prints the Python.Dll version, copyright information to Memo2 using this InitScript. import sys print(“Python Dll: “, sys.version) print(sys.copyright) print()
  • PythonGUIInputOutput1 component provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently
    executing Python script.
  • PythonModule1 with Module name spam is created. During PythonModule1Initialization a method spam_foo is added to the Module. And the definition of the method is included in the same unit file. Logically a python module is created and its methods were created using this PythonModule. Later this can be imported into your python script wherever necessary.
  • Memo1, used for providing the Python Script which imports spam (PythonModule1), accesses the method spam.foo and the method shows the output. Buttons to perform the execution, load script from, and save the script to file.
  • On Clicking Execute Script Button the script import spam print (spam.foo(‘hello world’, 1)) is executed.
<strong>Python4Delphi Demo5<strong>

Check some of the tutorials available for Python4Delphi here


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES