How To Use The Python Language In A C++ App

Python is hugely popular and has some really useful libraries. Python is particularly strong in the field of AI and machine learning (ML). If you are C++ developer, you might want your users to be able to analyze data with AI modules or frameworks written in the Python language in your applications. Let’s imagine you want the users to carry out a few button clicks to do some heavy AI analysis. That way you get the best of both worlds by having the raw speed and power of C++ which can run Python modules. Do you know, you can do this in a C++ IDE, very easily? In this post, we will show how you can integrate your C++ application with the Python language.

Why should I use the Python language in my C++ app?

In the programming world today, Python programming language is very popular – perhaps second only to C++. Python is easy to use, and it is more popular because of support from big companies like Google. One of the areas Python is particularly successful are with libraries and frameworks for AI technology and machine learning. It is a great object-oriented, interpreted, and interactive programming language. Python has very clear syntax. It has modules, classes, exceptions, very high-level dynamic data types, and dynamic typing. There are interfaces to many system calls and libraries, as well as to various windowing systems. New built-in Python modules are easily written in C or C++. Python is also usable as a scripting extension for applications written in other languages that benefit from its ease of use. Python is free to use. More information can be found on their official web page https://www.python.org/

We should note that Python can be used inside Delphi projects and this prime number function is tested before in Delphi and in Python here https://www.youtube.com/watch?v=aCz5h96ObUM with Kiriakos Vlahos. Here, we were inspired by this video, and we try to do the same tests on the same function in C++. Finding Prime Numbers in C++ is explained in detailed well in this Easily Learn To Find Prime Numbers In Modern C++ post here

Why should I use C++ instead of Python in an app?

C and C++ are the fastest and one of the most powerful programming languages and it is easy to do computations as fast as possible. For example, C++ Builder is a great IDE, includes compilers for Win32, Win64, Android, and iOS. C++Builder has both CLANG Enhanced C/C++ Compiler and Embarcadero’s new Bcc C/C++ Compiler. It also features a modern, high-productivity RAD Studio IDE, debugger tools, and enterprise connectivity to accelerate cross-platform UI development. You can develop GUI based applications easily, as it comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs. There is a free C++ Builder Community Edition that can be used by students, beginners, and startups with limitations too. 

In our different Prime Numbers Tests (I found that this test is a good test which has integers, floats, floating functions, logical functions, and iterations) on the same PC System, our tests show that C++ is about 9.4 times faster than Python on counting prime numbers in 64 bits and about 17.8 times faster in 32 bits. The calculating of prime numbers is good to use some basic logical operations and mathematical functions. The procedure includes logical if() clauses, for() loops, some mathematical procedures (floor, sqrt etc.), that show C++ is very fast in mathematical operations. These kinds of tests can be done in different operations to compare languages. Even the best-written Python cannot come anywhere near to the speed of a similar C++ app.

In some other examples, Python maybe 20, 50, 100, 200 times slower. Don’t forget that Python uses C++ modules ‘under the hood’. When you run some commands it uses these modules, which makes it faster in its modular functions, i.e. in AI applications.

Where can I use Python language in my C++ app?

If you want to use Python features in your applications, there is a simple way to do it. First, let’s give an example,

For example, you developed a very complex C++ application that gets instant images from different cameras with different connection modules, let’s assume that users can edit, and do many operations on these images by your application, there are user login and security modules, database connections, etc., etc. And, at some point, you want your users to know what is in that picture or you want your application to warn if there is a human (or car, or a wild animal) in that view. To do this you have 3 different choices;

  • You can develop your own Deep Learning library in C++
  • You can use 3rd party Deep Learning libraries in C++ (open source or paid)
  • You can use Python and its frameworks, and adapt some examples from the internet

Let’s assume that, you found some simple Python codes that can learn and analyze from these images and you want to use this Python code in your beautifully skinned modern high-res C++ app. In this situation, the best to do is to follow the 3rd choice. In this post below, we explain 3rd choice, which is simple and easy to use the latest Python and easy to run the latest Python codes you found.

How to use the Python language in a C++ app?

First, you need an appropriate version of Python with the Python codes you want to use. Python is free to use and more information can be found on their official web page https://www.python.org/, install it from there. Then may need to install more modules or frameworks to your Python. Be sure that the Python compiler is running in any paths in the command console. Now let’s give a very simple and modern C++ Builder VCL example.

  1. Create a new C++ Builder Application (VCL or FMX)
  2. Add a Memo (TMemo) to enter some Python codes, this will be Memo1
  3. Add another Memo (TMemo) for the outputs of Python codes, this will be Memo2
  4. Add a Button (TButton) to run Python codes, this will be Button1, set its caption or text to “Save and Run”
  5. Double Click to Button1, this will generate Button1Click()
  6. Add lines below inside that Button1Click() event,
How To Use The Python Language In A C++ App The RAD Studio IDE

When the user clicks this button, first, we will save Python codes written in Memo1 to a tmp.py file

Then, we will run this tmp.py Python file by using system() command and output to tmp.out,

Finally, we will read the output from tmp.out,

More on how to use the Python language in a C++ application

Briefly, these are 3 steps together in C++ Builder application. It’s very easy.

Here is a VCL example that runs python code:

How to use the Python language in a C++ VCL application?

Here is a full VCL example in C++ Builder,

How to use the Python language in a cross-platform C++ FMX application?

Here is a FMX example in C++ Builder.

How to use the Python language in a C++ console application?

Here is a simple console application that can be used in any windows compiler like C++ Builder, Dev-C++, MSVC, VSCode, GNU gcc g++, etc.

How To Use The Python Language In A C++ App The C++ Builder Logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


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

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?

C++C++17C++20Introduction to C++Language FeatureLearn C++Syntax

Learn How To Use Clamp (std::clamp) In Modern C++ 17 and Beyond