Source code compilation and installation of embedded ARM 64 bit platform PyQt5 and its call configuration in virtual environment

Keywords: Python Embedded system ARM GUI PyQt5

preface

On the embedded ARM 64 bit platform, when PyQt5 is installed in pip mode, because there is no compiled whl package of aarch64 in pip source, it is necessary to download the source package of PyQt5 and sip for automatic compilation, which usually fails to compile. Although sudo apt get install python3-PyQt5 can be installed successfully, the PyQt5 package installed in APT mode is installed in usr / bin / python3 / dist packages by default, The virtual environment created by virtualenv can only call the installed modules through the site packages path, so PyQt5 is installed on the embedded platform by source code compilation.

Tip: the following is the main body of this article for reference only

1, Introduction to PyQt5 and SIP

PyQt5 is a product of Riverbank company. It is divided into open source version and commercial version. The open source version contains all functions.
SIP is a tool for converting C/C + + libraries into Python bindings. SIP was originally developed to develop PyQt. Now it can also be used to convert any C/C + + library into Python bindings. PyQt5 relies on SIP packages.

2, Development and deployment environment

development environment Software version / configuration
Development board modelFirefly AIO-3399 ProC development board (3+16GB)
Development board operating systemUbuntu 18.04LTS
Development board firmware versionAIO-RK3399PROC-UBUNTU18.04-GPT-20200525-1016.img
Python versionPython 3.7.10
PyQt5 version5.15.2
SIP version4.19.25
Computer remote softwareXshell 7&Xftpd 7

Note: the PyQt5 version should correspond to the SIP version. Errors will occur when compiling the source package downloaded directly from the pip source,
You need to download the SIP package from the official website of riverbank Download link , PyQt5 package Download link

3, PyQt5 and SIP installation steps

1. Install Qt5 default

When compiling PyQt5 from the source code, qmake needs to be installed in the / usr/lib/qt5/bin / path of the system. Qt5 default package needs to be installed first. At this time, qmake will be installed by default.

user@admin:~$ sudo apt-get install qt5-default
...
The following [new] packages will be installed:
  libdrm-dev libegl1-mesa-dev libgl1-mesa-dev libgles1 libgles2-mesa-dev
  libglu1-mesa-dev libglvnd-core-dev libglvnd-dev libopengl0 libqt5concurrent5
  libqt5opengl5-dev libqt5sql5 libqt5sql5-sqlite libqt5test5 libqt5xml5
  libwayland-bin libwayland-dev libx11-xcb-dev libxcb-dri2-0-dev
  libxcb-dri3-dev libxcb-glx0-dev libxcb-present-dev libxcb-randr0-dev
  libxcb-shape0-dev libxcb-sync-dev libxcb-xfixes0-dev libxshmfence-dev
  libxxf86vm-dev mesa-common-dev qt5-default qt5-qmake qt5-qmake-bin
  qtbase5-dev qtbase5-dev-tools qtchooser x11proto-xf86vidmode-dev
  ...
  #Verify that qmake is installed
user@admin:~$ qmake --version
QMake version 3.1

2. Configure python and virtual environment

according to Firefly AIO-3399ProC development board installation RKNN Toolkit 1.6.0 development environment This article configures python and virtual environment.
Switch to the virtual environment from the command line:

user@admin:~$ virtualenv -p /usr/bin/python3.7 pyqt5
user@admin:~$ source pyqt5/bin/activate
(pyqt5) user@admin:~$ pip3 list
Package       Version
------------- -------
pip           21.3.1
pkg_resources 0.0.0
setuptools    59.2.0
wheel         0.37.0

3. Source code compilation and installation of SIP package

Install and compile the required packages

sudo apt-get install cmake gcc g++
pip3 install --upgrade pip
pip3 install wheel setuptools

Compiling SIP packages

(pyqt5) user@admin:~$ cd ./pyqt5/
(pyqt5) user@admin:~$ tar zxvf sip-4.19.25.tar.gz
pyqt5) user@admin:~/pyqt5$ cd ./sip-4.19.25
(pyqt5) user@admin:~/pyqt5/sip-4.19.25$ sudo python3 configure.py --sip-module PyQt5.sip
This is SIP 4.19.25 for Python 3.7.12 on linux.
The SIP code generator will be installed in /usr/bin.
The sip.h header file will be installed in /usr/include/python3.7m.
The PyQt5.sip module will be installed in /usr/lib/python3/dist-packages/PyQt5.
The sip.pyi stub file will be installed in
/usr/lib/python3/dist-packages/PyQt5.
The default directory to install .sip files in is /usr/share/sip.
Creating sipconfig.py...
Creating top level Makefile...
Creating sip code generator Makefile...
Creating sip module Makefile...
(pyqt5) user@admin:~/pyqt5/sip-4.19.25$ sudo make
...
(pyqt5) user@admin:~/pyqt5/sip-4.19.25$ sudo make install
make[1]: Enter directory“/home/user/pyqt5/sip-4.19.25/sipgen"
cp -f sip /usr/bin/sip
cp -f /home/user/pyqt5/sip-4.19.25/siplib/sip.h /usr/include/python3.7m/sip.h
make[1]: Leave directory“/home/user/pyqt5/sip-4.19.25/sipgen"
make[1]: Enter directory“/home/user/pyqt5/sip-4.19.25/siplib"
cp -f sip.so /usr/lib/python3/dist-packages/PyQt5/sip.so
strip /usr/lib/python3/dist-packages/PyQt5/sip.so
cp -f /home/user/pyqt5/sip-4.19.25/sip.pyi /usr/lib/python3/dist-packages/PyQt5/sip.pyi
make[1]: Leave directory“/home/user/pyqt5/sip-4.19.25/siplib"
cp -f sipconfig.py /usr/lib/python3/dist-packages/sipconfig.py
cp -f /home/user/pyqt5/sip-4.19.25/sipdistutils.py /usr/lib/python3/dist-packages/sipdistutils.py
/usr/bin/python3 /home/user/pyqt5/sip-4.19.25/mk_distinfo.py "" /usr/lib/python3/dist-packages/PyQt5_sip-4.19.25.dist-info installed.txt

4. Source code compilation and installation of PyQt5 package

PyQt5 takes a long time to compile the source code. If there is no error, just wait patiently.

(pyqt5) user@admin:~$ cd ./pyqt5/
(pyqt5) user@admin:~$ tar zxvf PyQt5-5.15.2.tar.gz
(pyqt5) user@admin:~/pyqt5$ cd ./PyQt5-5.15.2
(pyqt5) user@admin:~/pyqt5/PyQt5-5.15.2$ sudo python3 configure.py
Querying qmake about your Qt installation...
Determining the details of your Qt installation...
This is the GPL version of PyQt 5.15.2 (licensed under the GNU General Public
License) for Python 3.7.12 on linux.

Type 'L' to view the license.
Type 'yes' to accept the terms of the license.
Type 'no' to decline the terms of the license.

Do you accept the terms of the license? yes
...
(pyqt5) user@admin:~/pyqt5/PyQt5-5.15.2$ sudo make -j4
...
g++ -Wl,--version-script=pyrcc.exp -Wl,-O1 -shared -o libpyrcc.so sippyrccRCCResourceLibrary.o sippyrcccmodule.o rcc.o  -lQt5Xml -lQt5Core -lpthread  
cp -f libpyrcc.so pyrcc.so
make[1]: Leave directory“/home/user/pyqt5/PyQt5-5.15.2/pyrcc"
cd Qt/ && ( test -e Makefile || /usr/lib/qt5/bin/qmake -o Makefile /home/user/pyqt5/PyQt5-5.15.2/Qt/Qt.pro ) && make -f Makefile 
make[1]: Enter directory“/home/user/pyqt5/PyQt5-5.15.2/Qt"
gcc -c -pipe -O2 -fno-exceptions -Wall -W -D_REENTRANT -fPIC -DSIP_PROTECTED_IS_PUBLIC -Dprotected=public -DQT_NO_EXCEPTIONS -DQT_NO_DEBUG -DQT_PLUGIN -I. -I. -isystem /usr/include/python3.7m -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ -o sipQtcmodule.o sipQtcmodule.c
rm -f libQt.so
g++ -Wl,--version-script=Qt.exp -Wl,-O1 -shared -o libQt.so sipQtcmodule.o  -lpthread  
cp -f libQt.so Qt.so
make[1]: Leave directory“/home/user/pyqt5/PyQt5-5.15.2/Qt"
(pyqt5) user@admin:~/pyqt5/PyQt5-5.15.2$ sudo make install 
...
/usr/lib/qt5/bin/qmake -install qinstall /home/user/pyqt5/PyQt5-5.15.2/QtPrintSupport.pyi /usr/lib/python3/dist-packages/PyQt5/QtPrintSupport.pyi
/usr/lib/qt5/bin/qmake -install qinstall /home/user/pyqt5/PyQt5-5.15.2/QtSql.pyi /usr/lib/python3/dist-packages/PyQt5/QtSql.pyi
/usr/lib/qt5/bin/qmake -install qinstall /home/user/pyqt5/PyQt5-5.15.2/QtTest.pyi /usr/lib/python3/dist-packages/PyQt5/QtTest.pyi
/usr/lib/qt5/bin/qmake -install qinstall /home/user/pyqt5/PyQt5-5.15.2/QtWidgets.pyi /usr/lib/python3/dist-packages/PyQt5/QtWidgets.pyi
/usr/lib/qt5/bin/qmake -install qinstall /home/user/pyqt5/PyQt5-5.15.2/QtXml.pyi /usr/lib/python3/dist-packages/PyQt5/QtXml.pyi
/usr/lib/qt5/bin/qmake -install qinstall /home/user/pyqt5/PyQt5-5.15.2/QtDBus.pyi /usr/lib/python3/dist-packages/PyQt5/QtDBus.pyi
/usr/bin/python3 /home/user/pyqt5/PyQt5-5.15.2/mk_distinfo.py "" /usr/lib/python3/dist-packages/PyQt5-5.15.2.dist-info installed.txt
...

Four. The configuration of calling PyQt5 in virtual environment

Enter the path of PyQt5 source code compilation and installation. After packaging, move it to the dist packages of the virtual environment and unzip it.

(pyqt5) user@admi:~/pyqt5/PyQt5-5.15.2$ cd /usr/lib/python3/dist-packages/
(pyqt5) user@admin:/usr/lib/python3/dist-packages$ sudo tar zcvf pyqt5.tar.gz ./PyQt5/
./PyQt5/
./PyQt5/pyrcc.so
./PyQt5/QtDBus.pyi
./PyQt5/QtSql.so
./PyQt5/pyrcc_main.py
./PyQt5/QtSql.pyi
./PyQt5/QtNetwork.pyi
./PyQt5/QtNetwork.so
./PyQt5/sip.so
./PyQt5/QtGui.so
./PyQt5/_QOpenGLFunctions_4_1_Core.so
./PyQt5/QtDBus.so
./PyQt5/QtWidgets.pyi
./PyQt5/QtTest.so
./PyQt5/QtOpenGL.so
./PyQt5/QtOpenGL.pyi
./PyQt5/pylupdate.so
./PyQt5/_QOpenGLFunctions_2_1.so
./PyQt5/uic/
./PyQt5/uic/properties.py
./PyQt5/uic/exceptions.py
./PyQt5/uic/Loader/
./PyQt5/uic/Loader/loader.py
./PyQt5/uic/Loader/qobjectcreator.py
./PyQt5/uic/Loader/__init__.py
./PyQt5/uic/pyuic.py
./PyQt5/uic/Compiler/
./PyQt5/uic/Compiler/misc.py
./PyQt5/uic/Compiler/qtproxies.py
./PyQt5/uic/Compiler/qobjectcreator.py
./PyQt5/uic/Compiler/indenter.py
./PyQt5/uic/Compiler/proxy_metaclass.py
./PyQt5/uic/Compiler/__init__.py
./PyQt5/uic/Compiler/compiler.py
./PyQt5/uic/port_v2/
./PyQt5/uic/port_v2/proxy_base.py
./PyQt5/uic/port_v2/string_io.py
./PyQt5/uic/port_v2/as_string.py
./PyQt5/uic/port_v2/__init__.py
./PyQt5/uic/port_v2/ascii_upper.py
./PyQt5/uic/driver.py
./PyQt5/uic/icon_cache.py
./PyQt5/uic/__init__.py
./PyQt5/uic/widget-plugins/
./PyQt5/uic/widget-plugins/qtcharts.py
./PyQt5/uic/widget-plugins/qaxcontainer.py
./PyQt5/uic/widget-plugins/qtwebkit.py
./PyQt5/uic/widget-plugins/qtquickwidgets.py
./PyQt5/uic/widget-plugins/qtwebenginewidgets.py
./PyQt5/uic/widget-plugins/qtprintsupport.py
./PyQt5/uic/widget-plugins/qscintilla.py
./PyQt5/uic/port_v3/
./PyQt5/uic/port_v3/proxy_base.py
./PyQt5/uic/port_v3/string_io.py
./PyQt5/uic/port_v3/as_string.py
./PyQt5/uic/port_v3/__init__.py
./PyQt5/uic/port_v3/ascii_upper.py
./PyQt5/uic/uiparser.py
./PyQt5/uic/objcreator.py
./PyQt5/_QOpenGLFunctions_2_0.so
./PyQt5/QtPrintSupport.so
./PyQt5/QtXml.so
./PyQt5/__init__.py
./PyQt5/sip.pyi
./PyQt5/pylupdate_main.py
./PyQt5/QtGui.pyi
./PyQt5/QtXml.pyi
./PyQt5/QtWidgets.so
./PyQt5/QtCore.pyi
./PyQt5/QtPrintSupport.pyi
./PyQt5/QtTest.pyi
./PyQt5/Qt.so
./PyQt5/QtCore.so
(pyqt5) user@admin:/usr/lib/python3/dist-packages$ sudo cp ./pyqt5.tar.gz /home/user/pyqt5/lib/python3.7/site-packages/
(pyqt5) user@admin:~$ cd /home/user/pyqt5/lib/python3.7/site-packages/
(pyqt5) user@admin:~/pyqt5/lib/python3.7/site-packages$ tar zxvf pyqt5.tar.gz
...

5, Test the version and function of PyQt5

(pyqt5) user@admin:~$ python3
Python 3.7.12 (default, Sep 10 2021, 00:21:48) 
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import PyQt5
>>>

The following are two python scripts used to test the version and function of PyQt5

#Test the versions of Qt5,SIP and PyQt5
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QT_VERSION_STR
from PyQt5.Qt import PYQT_VERSION_STR
from sip import SIP_VERSION_STR
 
if __name__=='__main__':
    import sys
    app=QApplication(sys.argv)
    print("Qt5 Version Number is: {0}".format(QT_VERSION_STR))
    print("PyQt5 Version is: {}".format(PYQT_VERSION_STR))
    print("Sip Version is: {}".format(SIP_VERSION_STR))
 
    sys.exit(app.exec_())
    

After saving as pyqt5version.py file, execute in the terminal:

(pyqt5) user@admin:~/pyqt5$ python3 pyqt5version.py 
Qt5 Version Number is: 5.9.5
PyQt5 Version is: 5.15.2
Sip Version is: 4.19.25
#Test the function of PyQt5
import sys
from PyQt5 import QtWidgets, QtCore

app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
widget.resize(360, 360)
widget.setWindowTitle("Hello, World")
widget.show()
sys.exit(app.exec_())

After being saved as pyqt5test.py file and executed in the terminal, the following window appears, indicating that PyQt5 functions normally.

summary

At this point, according to the above steps, we compile and install PyQt5 on the embedded ARM 64 bit platform, and successfully call PyQt5 to complete the GUI design in the virtual environment created by virtualenv.
Reference article:
raspbian manual compilation and installation of pyqt5 and pyqt5.sip for raspberry pie
Python 3.9 source code compilation and installation pyqt5
Python Qt GUI and data visualization programming

Posted by majik_sheff on Fri, 19 Nov 2021 09:44:17 -0800