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

Learn A Faster Way To Make A Python Module As A DLL Using Python4Delphi Sample App

Sometimes we may need to share the functionalities as DLL and we know, creating a DLL in Delphi is a simple task. We learned how to create Python Module and add methods to it in Delphi. How about making a python module as DLL using Python4Delphi and import this python module in another application? This post will guide you to do that. You can also use Python4Delphi with C++Builder.

Python4Delphi Demo9 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 Demo9 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 Demo9 App: No visual components were added to the library however the following class were instantiated by using PythonEngine.pas

  • TPythonEngine: A collection of relatively low-level routines for communicating with Python, creating Python types in Delphi, etc. It’s a singleton class.
  • 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 Demo9 sample project from the extracted GitHub repository ..\Python4Delphi\Demos\Demo09.dproj. Open this project in RAD Studio 10.4.1 and run the application.

Implementation Details:

This Sample contains one library project(demodll) and a VCL application project(Demo09).

Demodll contains a procedure PyInit_demodll which will create an instance of TPythonEngine and TPythonModule. PythonEngine loads the python DLL using the LoadDll procedure. PythonModule named ‘demodll’ will have a method to add 2 integers. See the code below. When building this project, demodll.pyd is created.

In the VCL Application, this Python Module Dll is imported and executed using the below script. The components like TPythonEngine and TPythonGUIInputOutput were used to execute the script and show the result to the Memo2. Note: This sample is referring to version 3.7 in both Dll and Exe. Ensure your python version matches the code. The Screens and code in this post use the 3.9 version.

<strong>Python4Delphi Demo09<strong>

Check some of the tutorials available for Python4Delphi here

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

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