DelphiLearn PythonPythonPython GUIRAD Studio

Learn A Powerful Python Concept: Modules And Packages With A Delphi Windows GUI App

blogbanner3 3

Like many other programming languages, Python supports modularity. That is, we can break large code into smaller and more manageable pieces. And through modularity, Python supports code reuse. We can import modules in Python into our programs and reuse the code therein as many times as we want.

This post will demonstrate how to run another powerful Python feature in Python4Delphi with RAD Studio:  Modules and Packages in Python GUI and gets the output.

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.

Python4Delphi Demo1 Sample App shows how to run a Python Script by typing the python code in a Memo, execute and populate the result in another Memo. You can find the Demo1 source on GitHub.

First, run the Python GUI in the Demo1 App:

0 rundemo1
Open Demo01dproj

1. What is a Python Module?

Consider a module to be the same as a code library or a file. A module may contain a set of functions, classes, lists, or anything you want to include in your application.

Modules in Python can be of two types:

  • Built-in Modules.
  • User-defined Modules.

1.1. Built-in Modules in Python

One of the many superpowers of Python is that it comes with lots of built-in modules. This Python’s “rich standard library” is very extensive, offering a wide range of reusable code. The library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming. Some of these modules are explicitly designed to encourage and enhance the portability of Python programs by abstracting away platform-specifics into platform-neutral APIs.

To name a few, Python contains modules like “os”, “sys”, “datetime”, “random”. We can import and use any of the built-in modules whenever we like in our program.

Let’s try to generate a random integer between 0 and 1000, using Python “random” module:

See the output in Python4Delphi:

1 1 builtinmodules 9723895
Running Example of Built in Modules in Python random Module in Python4Delphi

1.2. User-Defined Modules in Python

On the other hand, another superpower of Python is that it lets you take things in your own hands. We can create our own functions and classes, put them inside modules, and voila! We can now include hundreds of lines of code into any program just by writing a simple import statement.

To create a module, just put the code inside a .py file. Let’s create one!

No expected output in Python4Delphi Python GUI, save our script to “”:

1 2 userdefinedmodules 9680305
User Defined Modules in Python in this Example mymodule in Python4Delphi

We will import in the Section 2.


2. Importing Modules in Python

Now we can use the module we just created or any built-in modules easily, by using the import or from statement.

2.1. import Statement

To make sure everything is work well, before import our user-defined “mymodule”, in the Python GUI from Python4Delphi in RAD Studio, run this script first:

While “C:/Users/ASUS/Documents” could be changed to your current working directory.

Import our mymodule, and call the greeting function:

Let’s see it in action in Python4Delphi:

2 1 importstatement 7121015
import Statement in Python4Delphi

2.2. Variables in Module

As already described, the module can contain functions, also variables of all types (arrays, dictionaries, objects, etc). Let’s type this example, and save it to the file

And then, import the module named mypymodule, and access the person1 dictionary:


2 2 variablesinmodule 3234767
Learn Variables in Module with Python4Delphi

2.3. Re-naming a Module (Using import…as Statement)

We can create an alias when you import a module, by using the as keyword. For example, create an alias for mymodule called mx:


2 3 renamingmodule 4532050
Re naming a Module Using importas Statement in Python4Delphi

2.4. from Statement

We can choose to import only parts from a module, by using the from keyword.

See the output in Python4Delphi:

2 4 fromstatement 7268897
from Statement in Python4Delphi

2.5. Import Everything from Python Module

If we need to import everything from a module and we can use the * symbol, like this:

See the output in Python4Delphi:

2 5 importeverything 6833623
Import Everything from a Python Module in Python4Delphi

2.6. Python dir() Function

The dir() function will return the names of all the available properties and methods present in a module.

See the output in Python4Delphi:

2 6 dirfunction 1252188
Python dir Function in Python4Delphi


3. Reloading Modules in Python

If you have already imported a module but need to reload it, use the reload() method of importlib. This is intended to be used in cases when you edit a source file of a module and need to test it without leaving Python. In Python 3.0 and above, you need to import importlib standard library module to make use of this function.

There is no expected output upper memo of our Python GUI:

3 reloadingpythonmodules 3681472
Reloading Python Modules in Python4Delphi

Modules are a great deal in programming languages as they give us the ability to reuse code in a much more manageable way. They say, “Python comes with batteries included”. By batteries, they mean modules. Modules are everything you need to get going. All you have to do is code.

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And NumPy Library?


Unlock the Power of Python for Deep Learning with Generative Adversarial Networks (GANs) - The Engine behind DALL-E

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

Leave a Reply

Your email address will not be published. Required fields are marked *