📜 ⬆️ ⬇️

Build a project in python3 & PyQT5 for Windows using PyInstaller

The reason for writing the article was a huge number of constantly emerging questions for newbies with the following content: "How to build a project with pyqt5", "Why it doesn't work", "Which tool to choose", etc. Today we will learn how to collect projects without torment and dancing with a tambourine.



Somehow I had to write a small desktop application. Python was chosen as the programming language for development, since it was ideally suited for solving my problem. The standard Python library already includes the tkinter library, which allows you to create GUIs. But the problem with tkinter is that little attention is devoted to this library, and finding a course, book or FAQ on it is quite difficult to find on the Internet. Therefore, it was decided to use a more powerful, modern and functional Qt library, which has links to the python programming language in the form of the PyQT5 library. You can read more about PyQT here . As an example, I will use the code:


#!/usr/bin/python3 # -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtGui import QIcon class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 300, 220) self.setWindowTitle('Icon') self.setWindowIcon(QIcon('web.png')) self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) 

If you are a more or less experienced developer, then you understand that you cannot run python code without an interpreter. And I would like to give every user the opportunity to use the program. This is where special libraries come to our aid and allow you to build projects in .exe, which you can then run as a normal application without any problems.


There are a large number of libraries that allow this to be done, among which the most popular are: cx_Freeze, py2exe, nuitka, PyInstaller, and others. Quite a lot are written about each. But I must say that many of these solutions allow you to run the code only on a computer with a pre-installed interpreter and pyqt5. I do not think that the user will bother and set himself additional packages and programs. I hope you understand that running a program on a dev-environment and the user is not the same thing. It should also be noted that each solution had its own problems: one did not start, the other collected what it could not launch, the third refused to do anything at all.


After a long dance with a tambourine and active googling, I still managed to build the project using pyinstaller, into a fully working application.


Little about pyinstaller


Pyinstaller builds the python application and all dependencies in one package. The user can run the application without installing the python interpreter or any modules. Pyinstaller supports python 2.7 and python 3.3+ and such libraries as: numpy, PyQt, Django, wxPython and others.


Pyinstaller has been tested on Windows, Mac OS X and Linux. Anyway, this is not a cross-platform compiler: to make an application under Windows, do it on Windows; To make a Linux application, do it on Linux, etc.


PyInstaller has been used successfully with AIX, Solaris, and FreeBSD, but has not been tested.


You can read more about PyInstaller here: documentation .


In addition, after the assembly, the application weighed only about 15 mb. By the way, this is an advantage of the pyinstaller, since it does not collect everything, but only the necessary. Similar libraries gave the result for 200-300 mb.


Getting Started


Before we start building, we need to install the necessary libraries, namely pywin32 and the pyinstaller itself:


 pip install pypiwin32 pip install pyinstaller 

To make sure that everything is set up normally, enter the command:


 pyinstaller --version 

the pyinstaller version should be displayed. If everything is set correctly, go ahead.


In the project folder, run cmd and type:


  pyinstaller myscript.py 

Actually this is the simplest team that will assemble our project.
The pyinstaller command syntax is:


pyinstaller [options] script [script ...] | specfile


The most commonly used options are:


--onefile - build into one file, i.e. .dll files are not written.
--windowed - when the application starts, the console will appear.
--noconsole - when launching the application, the console will not appear.
--icon = app.ico - add an icon to the window.
- paths - the ability to manually set the path to the required files, if pyinstaller
can't find them (for example: --paths D: \ python35 \ Lib \ site-packages \ PyQt5 \ Qt \ bin)


PyInstaller analyzes the myscript.py file and does the following:


  1. Writes a myscript.spec file in the same folder as the script.
  2. Creates a build folder in the same folder as the script.
  3. Writes some logs and work files to the build folder.
  4. Creates a dist folder in the same folder as the script.
  5. Writes the executable file to the dist folder.

As a result, our team will look like this:


 pyinstaller --onefile --icon=name.ico --noconsole myscript.py 

After running the program, you will find two folders: dist and build. Actually in the dist folder is our application. Subsequently, the build folder can be safely removed, it does not affect the performance of the application.


Thanks for attention. I hope the article was useful to you.


')

Source: https://habr.com/ru/post/325626/


All Articles