Nuitka is a Python compiler written in Python and is capable of taking a Python script and create an executable from it. When using Pyside6, I ran into a problem of missing 'qml' resources. This article describes how to add the missing files to the app package after compiling.

Use Nuitka to compile a macOS executable from a Python Pyside6 app.

Start by creating a project and a virtual environment.

mkdir pyside6nuitka
cd pyside6nuitka/
python3 -m venv env

Activate the virtual environment and pip install nuitka and pyside6.

source env/bin/activate
pip install nuitka
pip install pyside6

Here is a pip list directory:

pip list
Package    Version
---------- --------
Nuitka     0.6.16.2
pip        20.2.3
PySide6    6.1.2
setuptools 49.2.1
shiboken6  6.1.2

Create main.py and enter the following code to create a window:

import sys
from PySide6.QtWidgets import QApplication
from PySide6.QtWidgets import QMainWindow
from PySide6.QtGui import QScreen

app = QApplication(sys.argv)
mainwindow = QMainWindow()
mainwindow.setGeometry(0, 0, 800, 600)
mainwindow.show()

app.exec()

Run python3 main.py and see the result:

empty window

Compile an executable with Nuitka

python3 -m nuitka --follow-imports --enable-plugin=pyside6 --standalone main.py

The following question is asked:

Nuitka will make use of ccache to speed up repeated compilation.
Is it OK to download and put it in '/Users/loek/Library/Application Support/Nuitka/ccache/v4.2.1'.
No installer needed, cached, one time question.
Proceed and download? [Yes]/No

I Answer with n (No) and the compiling continues until the end:

Nuitka:INFO: Successfully created 'main.dist/main'.

ImportError, Library not loaded: QtQml

At this point, you can execute the binary with the command:

main.dist/main

An error occurs:

ImportError: dlopen(../dev/pyside6nuitka/./main.dist/PySide6/QtWidgets.so, 2): Library not loaded: @rpath/QtQml.framework/Versions/A/QtQml
Referenced from: ..dev/pyside6nuitka/main.dist/libpyside6.abi3.6.1.dylib
Reason: image not found

The problem is that qml files are missing in main.dist/main.

Solution

package

Result with Qt folder in main.dist/PySide6:

result

Execute again:

main.dist/main

This time it works.

empty window

I can copy main.dist to another machine and it works there too.

Written by Loek van den Ouweland on 2021-06-29.
Questions regarding this artice? You can send them to the address below.