aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/doc/deployment/deployment-nuitka.rst
blob: ceac2065a69b69231b16fd19f26fa48399d116fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|project| & Nuitka
##################

`Nuitka <https://nuitka.net/>`_ lets you compile your python application into a
stand-alone executable. Besides being a Python compiler which provides a fair
acceleration, it has the side-effect of acting as an installer as well.
Nuitka supports Linux, macOS and Windows.

For more details, see the `official documentation <https://nuitka.net/pages/overview.html>`_.

Preparation
===========

Install ``Nuitka`` via **pip** with the following command::

    pip install nuitka

After installation, the ``nuitka3`` binary is located in your virtual environment's ``bin/``
directory, or where your Python executable is located.
Alternatively, you can also run::

    python3 -m nuitka

to achieve the same effect.

Freeze an application
=====================

``Nuitka`` has many options that you can use. To list them all, run ``nuitka3 -h``.

To simply compile a project, you can run::

    nuitka3 <programname>

There are two main features:

* the option to place it in a directory containing the libraries
  (``--standalone``)
* the option to package the whole project (including shared libraries) into one executable file
  (``--onefile``)

If you use these options, you need to specify ``--plugin-enable=pyside6``.

Run an example
--------------

Now, consider the following script, named ``hello.py``::

    import sys
    import random
    from PySide6.QtWidgets import (QApplication, QLabel, QPushButton,
                                   QVBoxLayout, QWidget)
    from PySide6.QtCore import Slot, Qt

    class MyWidget(QWidget):
        def __init__(self):
            QWidget.__init__(self)

            self.hello = ["Hallo Welt", "你好,世界", "Hei maailma",
                "Hola Mundo", "Привет мир"]

            self.button = QPushButton("Click me!")
            self.text = QLabel("Hello World")
            self.text.setAlignment(Qt.AlignCenter)

            self.layout = QVBoxLayout()
            self.layout.addWidget(self.text)
            self.layout.addWidget(self.button)
            self.setLayout(self.layout)

            # Connecting the signal
            self.button.clicked.connect(self.magic)

        @Slot()
        def magic(self):
            self.text.setText(random.choice(self.hello))

    if __name__ == "__main__":
        app = QApplication(sys.argv)

        widget = MyWidget()
        widget.resize(800, 600)
        widget.show()

        sys.exit(app.exec())

You don't have to copy this script. You find it as ``examples/installer_test/hello.py``.

The command line to proceed looks like this::

    nuitka3 examples/installer_test/hello.py

This process creates an executable ``hello.bin`` and a directory hello.build that you
don't need. You can execute the binary directly.

In order to create a bundle which can be copied onto a machine without any pre-existing
installation, run::

    nuitka3 --standalone --plugin-enable=pyside6 examples/installer_test/hello.py

This creates an application ``hello.dist/hello`` that contains everything needed to run.

To run the application, go to ``hello.dist/`` and run the program::

    cd hello.dist
    ./hello

Use the ``--onefile`` option if you prefer to have everything bundled into one executable, without
the shared libraries next to it. First you need to install::

    pip install zstandard

for data compression. Then you can run::

    nuitka3 --onefile --plugin-enable=pyside6 examples/installer_test/hello.py

This process takes a bit longer, but in the end you have one executable ``hello.bin``::

    ./hello.bin