How to turn a Pyqt5 project into an executable?

Asked

Viewed 1,314 times

4

I would like to know what would be the best way to convert an application Pyqt5 to an executable application.

  • Is there any way to "compile" an application in Pyqt5?

  • Is there any way to create an installer for a Pyqt5 application?

Specifically, I am targeting Windows, but I would also like to know how it would be possible to create a Pyqt5 application for Linux.

  • 3

    The negative! You can explain there what is wrong?

  • You will need the python interpreter and dependencies compiled specifically for Windows/Linux. I recommend taking a look at this tool that seems to be able to package everything in one executable. http://www.pyinstaller.org/

  • Take a look at this article, has several options to do what you want ;)

5 answers

2

To Wiki Python recommends some tools that can do this. In particular there are two that suit what you want:

fman build system

Allows packaging executable applications in Pyqt5 and create installer for platforms Windows, Linux and Macos.

The official documentation on github provides a tutorial on the generation of installers.

pyinstaller

Just like the fma the pyinstaller provides an environment for packaging programs Python in applications, for platforms Windows, Linux, Mac OS X, Freebsd, Solaris and AIX.

To official documentation detail well the installer creation.

2


Before I start I need to mention important details, several answers as being Pyinstaller the solution, but this is only part of the resolution, what people need to understand is that Pyinstaller does not magically solve everything, this because Linux is not an operating system, Linux is a kernel, there are many operating systems "based" on the Linux kernel (GNU/Linux), that is, there are different distributions and most (if not almost all) are not compatible.

Yes my people, a program compiled in Debian will not work in Centos, despite being a community open-source great yet each group thinks its way, each distro follows a path, and perhaps this division in the community is their evil (but it is speculation on my part).

Besides being written in Python a program created in Pyinstaller will embark the Python of the version of Linux specific for the executable, I will give an example, I used the Pyinstaller in an Ubuntu with a Python that is installed in the system also, what will happen is that Pyinstaller will copy the python executable from the system (or virtualenv) to the "package" generated by Pyinstaller along with an executable version it will generate, so if you try to run it in a Centos or Debian it probably won’t work (even though debian and Ubuntu are "related").

Pyqt5 and basic deploy (for now on Windows only)

All right, I’ll first talk about some basic details, I’ll explain in Windows for now because I am still preparing the environment in my Centos and in my Debian to be able to put details accurately.

I highly recommend that you do the installation via pip, because then you will have the packages easier to control the packages and even to install them, but before anything I also recommend much that use virtualenv, more details on:

This is because packages used for a version of Pyqt as well as Pyinstaller can change, update and other situations like this, which can cause you to accidentally update an older project, so isolating a version of Pyqt and other packages for a project of this type can be interesting, so moving the machine, updating your global Python, will not affect your project.

To avoid beating around the bush I’m going to the basic explanation with step by step:

  1. Create the project folder, let’s call it exemplo, can do via explore.exe right-clicking/mouse "new folder" or via cmd with the command md exemplo)

  2. Open the command prompt (if it is not open) and navigate to the folder where you will create the project, in which case it is the folder exemplo:

     cd c:\Foo\Bar\exemplo
    

This is an example, logical that will type a real, absolute or relative path.

  1. Enter the command:

    python -m venv . env

The .env is a folder, generated by -m venv, it will contain a copy of the current Python you use

  1. Now it is necessary to start the virtual env, for that still in cmd and folder exemplo type this:

     .env\scripts\activate.bat
    
  2. Being in the virtual environment now you must install the necessary packages, in case I tried to install the most updated ones, but there was some problem, maybe it is a conflict/bug with Python 3.6 and 3.7, for that I installed specific versions that worked well (of course you can update this as you wish), the commands (necessary packages are):

     pip install PyQt5==5.9.2
     pip install PyInstaller==3.3.1
    
  3. Ready the environment is created, to test the Pyqt5 create a file called main.py and put this content in it:

     from PyQt5 import QtCore, QtGui, QtWidgets
    
     class Ui_MainWindow(object):
         def setupUi(self, MainWindow):
             MainWindow.setObjectName("MainWindow")
             MainWindow.resize(800, 600)
    
    
     if __name__ == "__main__":
         import sys
         app = QtWidgets.QApplication(sys.argv)
         MainWindow = QtWidgets.QMainWindow()
         ui = Ui_MainWindow()
         ui.setupUi(MainWindow)
         MainWindow.show()
         sys.exit(app.exec_())
    
  4. After saving the document run it to test:

     python main.py
    

An empty window should be displayed (as it is only an initial example)

  1. Now the moment of deploy so expected, using Pyinstaller run:

     pyinstaller --windowed main.py
    

This will generate the executable, in the example it will be in the folder c:/Foo/Bar/dist/main, the whole folder is needed to work.

In Linux and Mac OSX-based environments

Soon I will put examples like Centos and Debian and if possible Mac, at the moment I am somewhat unable, due to be something very laborious, but I already say that to generate an installation package I may have to use what is explained in the following links (I’ll add more as I can):

I don’t know if pyinstaller solves this part, I think it only manages the executable file (stand-alone executable)


Creating an installer

Unfortunately this is not easy, you can create at hand, with Pyinstaller same, or other executable that install, of course this will all be a little loose, so practical solutions are:

The Inno Setup is interesting, I promise to create a small "tutorial" here in the same answer of how to configure the .ini of it, placing icon and maybe even "scripts" that will be executed at the time of installation (to eventually in a customized way prepare the operating system for something necessary).

1

I used Pyinstaller to create the executable structure of the program.

The steps used were:

  • I installed Python 3.5, 32 bit version

  • I used Virtual Env, through the command python -m venv venv.

  • I activated the env\Scripts\activate.ps1 (in my case, I used Windows 10).

  • I installed the Pyqt5.9.2 and the PyInstaller 3.3.1 for pip.

  • I turned the remote pyinstaller --paths .\venv\site-packages\PyQt5\Qt\bin main.py. In my case, main.py is the main script of my application.

This took care of creating the executable structure, with all the dependencies.

I used that answer from SOEN to perform these steps and worked.

I will try to improve the answer to make the installer creation

0

I also use Qt5 with Python, but in Pyside 2. You can compile (package) to executable using Pyintaller. Pyintaller currently only supports Python 3.6. I do not recommend another, because Pyintaller is the most used and the most popular. So either you use Python 3.6 or wait a little while for Python 3.7 support to come out, as support is on the way. Here is a print of Pyside2 with Qt 5.11.1 inserir a descrição da imagem aqui

  • In my case, it went round with PyQt5==5.9.2 and PyInstaller==3.3.1, in accordance with the requirements of that repository: fbs-tutorial

  • Right, but which version of Python? 3.7? Because as far as I know, Pyinstaller does not yet support Python 3.7, only up to 3.6.

  • There in the repository speaks Python 3.5

  • 1

    Python 3.5 is already very old right.

  • Interesting. I’m talking about the same tool and I get -2 for the answer .. I remembered why I stopped contributing to Stack a long time ago.

  • 1

    I gave you a positive, your answer is correct, the Pyinstaller.

  • People here expect you to help with specific steps or code, preferably Compile so you can just copy and stop.

Show 2 more comments

-3

pyinstaller main.py --clean --onefile --windowed --noconsole --add-data "WebBrowserInterop.x64.dll;./" --add-data "WebBrowserInterop.x86.dll;./" --add-data "Microsoft.Toolkit.Forms.UI.Controls.WebView.dll;./" --add-data "Microsoft.Toolkit.Forms.UI.Controls.WebView.LICENSE.md;./" --icon "icon.ico"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.