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:
Table of Contents
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:
1 2 3 4 |
import random # We use the dot notation to call the randint function of random module print(random.randint(0, 1000)) |
See the output 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!
1 2 3 |
# My Python module def greeting(x): print("Hello,", x) |
No expected output in Python4Delphi Python GUI, save our script to “mymodule.py”:
We will import mymodule.py 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:
1 2 3 4 |
import os, sys my_lib_path = os.path.abspath('C:/Users/ASUS/Documents') sys.path.append(my_lib_path) |
While “C:/Users/ASUS/Documents” could be changed to your current working directory.
Import our mymodule, and call the greeting function:
1 2 3 |
import mymodule mymodule.greeting("Python4Delphi") |
Let’s see it in action 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 mypymodule.py:
1 2 3 4 5 |
person1 = { "name": "Adam", "age": 32, "country": "USA" } |
And then, import the module named mypymodule, and access the person1 dictionary:
1 2 3 4 |
import mypymodule a = mypymodule.person1["age"] print(a) |
Output:
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
:
1 2 3 4 |
import mypymodule as mx a = mx.person1["name"] print(a) |
Output:
2.4. from Statement
We can choose to import only parts from a module, by using the from
keyword.
1 2 3 |
from mypymodule import person1 print(person1["country"]) |
See the output 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:
1 2 3 4 |
from math import * print(7 * pi) print(sqrt(1764)) |
See the output 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.
1 2 3 |
import random print(dir(random)) |
See the output 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.
1 2 3 4 |
import random import importlib as imp imp.reload(random) |
There is no expected output upper memo of our Python GUI:
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.