Run Qt App On Mac
A couple of years back, I was developing an application for Ubuntu. For unspecified reasons, I wanted to develop the GUI in Qt/C, but implement all logic in Python. The default Python that came with Ubuntu at the time could not run my scripts, so I had to ship a compatible Python version along with my application. Basically, I had to embed the Python interpreter into my program, by linking. I have already compiled my application on my Mac computer, but I don't know if my application will work on other Mac computers. I don't have two Macs, also, (as far as I know) it's difficult to install Mac OS X on a virtual machine with 'legal' discs or without having to perform a process similar to building a Hackintosh, this means that I. The next step once your app is built is to add launch configuration(s) so you can debug (or just run) your executable(s). First, let’s get Visual Studio Code to create a launch configuration file for you by opening the command line (Ctrl+Shift+P), selecting “Debug: Open launch.json”, and then “C (Windows)”. Just install Qt 5.11.3, build your app using it and test on MacOS 10.14. As alternative you can provide two versions of your app - one built using Qt 5.11.3 and one using latest stable release. ReplyQuote2 1 ReplyLast reply.
A couple of years back, I was developing an application for Ubuntu. For unspecified reasons, I wanted to develop the GUI in Qt/C++, but implement all logic in Python. The default Python that came with Ubuntu at the time could not run my scripts, so I had to ship a compatible Python version along with my application. Basically, I had to embed the Python interpreter into my program, by linking to it, and then load it at runtime to run my Python scripts. As it turned out, my app was a flop, but at least I got to make a tutorial out of it.
In this tutorial, I’ll show you how to embed the Python interpreter in your own Qt/C++ applications. We’ll be creating an application that minifies CSS stylesheet files. Actually, only the GUI will be in Qt, the CSS minification will be handled by Python using rCSSmin—a CSS minifier written in Python. We’ll statically link the Python interpreter to our application, and distribute the Python standard library in an archive, which will be loaded at runtime. That’s the only external file we’ll be shipping with our application, since the rCSSmin module will be stored in our program’s executable using Qt’s resource system. Pretty cool stuff, right?
This tutorial was written for Ubuntu 15.04, nevertheless, much of the information is still useful for other Ubuntu releases, Linux distributions, Mac, or Windows.
Prepare the Development Environment
Install the development tools (typically come pre-installed):
$ sudo apt-get install build-essential
Install Qt Creator:
$ sudo apt-get install qtcreator
Install the Python static library and other development files:
$ sudo apt-get install python3-dev
Python Script
rCSSmin is a stand-alone application, however, even the simplest Python modules depend on Python’s standard library to run. Since Python’s standard library is very extensive, it’s not very efficient to pack and distribute the whole library with our Qt application. For that reason, we’ll create a script that determines rCSSmin dependencies, compiles them, and conveniently packs them in a zip archive. To further save space, we’ll compile rCSSmin to bytecode and store it in a binary file.
Create a file named compile.py
:
If you haven’t done so already, download rCSSmin, and uncompress it. To use the above script, execute:
$ ./compile.py PATH/TO/rcssmin.py
The script will output 3 files:
libpy34.zip
: A subset of the Python standard library in a zip archive (The suffix number could be different, depending on your Ubuntu release).rcssmin.py.codeobj
: The rCSSmin code object, serialized using Marshal.dep.txt
: A list of rCSSmin dependencies, as determined by the script.
Qt Application
Run Qt Creator and create a new Qt Widget Application. Name the project embedPython
.
Create a new directory named res
in your project’s root directory and copy rcssmin.py.codeobj
. In your project, add a new Qt Resource File named embedPython.qrc
. Open the resource file to add rcssmin.py.codeobj
with /
prefix.
Open embedPython.pro
and add the following lines at the end:
Python 3.4 is the version of Python shipped with Ubuntu 15.04. If you are running a different release, you should find out which Python version you have installed, and then modify embedPython.pro
accordingly:
$ find /usr/include/ -maxdepth 1 -name python*
Open embedPython.ui
in the form editor. Add 2 plainTextEdit widgets and a pushButton, as shown in the Figure below:
In your project, create a new class named PyRun
. We will use PyRun
to initialize the Python interpreter, load the rCSSmin module using Qt’s resource system, and execute the cssmin
function inside the rCSSmin module.
Open pyrun.h
:
Open pyrun.cpp
:
In our MainWindow class, we handle the pushButton click action, run cssmin
, and display the result in the plainTextEdit widget.
Open mainwindow.h
:
Open mainwindow.cpp
:
Finally, in main we create an instance of PyRun
, and then pass it to our mainwindow. Open main.cpp
:
Testing the Application
Before running your Qt application for the first time, make sure that you copy libpy34.zip
in the same directory as the embedPython
executable. Then, download a large CSS file, such as the CSS stylesheet of the jQuery Mobile framework, and copy-paste the CSS contents in the Input plainTextEdit widget. After clicking on Minify, you should see the minified text in the Output plainTextEdit, as shown below:
As mentioned earlier, the Python interpreter is statically linked to our application. To verify, you can use the ldd
command, which prints a list of all shared libraries required by a program. If the Python interpreter is indeed statically linked to our application, libpython3.4m.so.1.0
should not be in the ldd
list:
$ ldd embedPython
You can find all source code in this tutorial in my GitHub repository.
This page contains information about some of the tools that could be used to deploy PyQt applications on various platforms, typically in binary form.
The idea is that each solution should be listed along with a brief description and a link to its home page. The description should probably come from the solution's home page or documentation, so that it is described 'in its own words'. Further comments and links for each can be given after these fields.
Cross-Platform Solutions
The following tools are cross-platform, working on Windows and some flavours of Unix.
fman build system
https://build-system.fman.io
Creates stand-alone executables and installers for PyQt applications. Supports Windows, macOS and Linux.
PyInstaller
http://pyinstaller.python-hosting.com/
'PyInstaller is a program that converts (packages) Python programs into stand-alone executables, under Windows, Linux and Irix.'
A short How-To for using PyInstaller with PyQt on Mac OS-X:
PyInstaller On Mac OS X
cx_Freeze
http://starship.python.net/crew/atuining/cx_Freeze/index.html
'cx_Freeze is a set of utilities for freezing Python scripts into executables using many of the techniques found in Thomas Heller's py2exe, Gordon McMillan's Installer and the Freeze utility that ships with Python itself.'
bbfreeze
(Windows, Unix, but not Mac OS X)
http://cheeseshop.python.org/pypi/bbfreeze/
'bbfreeze creates stand-alone executables from python scripts. It's
similar in purpose to the well known py2exe_ for windows, py2app_ for OS X, PyInstaller_ and cx_Freeze_ (in fact ancient versions were based on cx_Freeze. And it uses the modulegraph_ package, which is also used by py2app).'
Mercurial repository: http://systemexit.de/repo/bbfreeze
Freeze
Mac Install Qt
The original freeze tool that embeds Python bytecode into executables is supplied with Python - look in the examples/Tools directory.
qmake
For applications that don't depend too much on many shared library modules other than the ones shipped with PyQt, it may be possible to take advantage of qmake's features and a simple launcher application to create binaries for different platforms.
Tools for Windows
The following tools are designed to produce executables for Windows. Visual timer app mac.
py2exe
http://www.py2exe.org/
'py2exe is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation.'
Comments:I highly recommend py2exe which can produce nice executables. Combined with InnoSetup, you get a full standard windows application and the user has no idea that the stuff was actually developed on Linux with Open Source technologies. -- Philippe Fremy
Mac Install Qt Designer
See the following links for more information about deploying PyQt applications with py2exe:
http://www.py2exe.org/index.cgi/Py2exeAndPyQt
http://mail.python.org/pipermail/python-list/2007-September/458364.html
ExeMaker
http://effbot.org/zone/exemaker.htm
'ExeMaker is a small tool that takes a Python script, copies it to a program directory, and creates a Windows EXE file in the same directory.'
Tools for Mac OS X
The following tools are designed to produce executables for Mac OS X.
py2app
http://undefined.org/python/#py2app
Uninstall Qt Mac
'A distutils extension which converts python scripts into executable Mac OS X applications, able to run without requiring an existing Python installation.'
Qt Designer For Mac
Other Resources
How can I create a stand-alone binary from a Python script? from the pyfaq