Writer: Muhammad Al-Hadi Al-Tayeb.
May God's peace, mercy and blessings be upon you. May God's blessings and peace be upon our noble Prophet.
Before starting, I would like to express my thanks and appreciation to my teacher, Salah Atair, who honored me with reviewing and correcting this article.
In our post today, we will discuss the process of converting a Python file into an executable file in the greatest detail.
We will divide this post into three lessons.
Simple file conversion, integrated project conversion, and solving the problem of large executable file size.
1- Lesson 1: Converting a simple file.
The library I will be using in this tutorial is the pyinstaller library.
The first thing we have to do is install this library like this:
pip install pyinstaller
The Python file that will accompany us throughout this post is called test.py.
At first our file will be simple and we will convert it like this:
In cmd we choose the path of our file and then write the following command:
pyinstaller -w –onefile test.py
-w means, after the conversion process is completed and we run it, the cmd screen will not appear with us, but if we need this screen (for example, if our file will print something for us), then we do not write -w.
–onefile means that we tell pyinstaller to put the executable file in a single file, and if we don't write this code, the resulting executable will be in multiple folders and files.
After the successful completion of the conversion process, we will enter the path of our file, and we will notice that there are new folders, as well as a file named test.spec, and this file we will return to in an advanced stage of this post, which means that we will find the executable file of our Python file in the folder named dist.
Lesson 2: Converting an integrated project into an executable file.
After explaining a simple file conversion process in our previous lesson, we will now talk about a more complex conversion process.
Let's assume that our simple Python file that we converted in the above, suppose that we developed it and became a multi-tasking program project and files, including data files, in this case the conversion process will not take place in the same way as before.
During the previous conversion process, we found a new file named test.spec in the path of our file. What does this file contain and how do we deal with it?
This file contains the settings and properties of our project, and it can be opened with notepad, and we will find its content similar to the following:
–mode: python ; coding: utf-8 -–
block_cipher = None
a = Analysis(
['test.py'],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
hookconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = exe(
pyz,
a. scripts,
a. binaries,
a. zipfiles,
a. datas,
[],
name='test',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
We can make some modifications to this file, and we will modify one thing, and I advise not to modify anything unless you are confident and understand the required accuracy, what you are editing, and what you want from your modifications.
In the previous simple compilation, pyinstaller automatically created the test.spec file, but in the “advanced” compilation we are going to do now, we will divide it into two separate phases:
* First we will create the test.spec file for our project
Then we convert the test.spec file into an executable
Between the two stages we will make the modifications we want to the test.spec file.
We will make the modifications after creating the test.spec file and before converting it into an executable.
1- We create the test.spec file for our project as follows:
Of course, in cmd, we must always choose the path of our project.
pyi-makespec -w -onefile test.py
-w and –onefile are the ones we explained in the previous conversion process in our first lesson.
Microsoft Windows [version 10.0.19044.1706] (c) Microsoft Corporation. All of our reserves.
C:\Users\hedid\Desktop\test>pyi-makespec -w –onefile test.py
Wrote C:\Users\hedid\Desktop\test\test.spec.
Now run pyinstaller.py to build the executable.
C:\Users\hedid\Desktop\test>
Then we open the test.spec file that we will find in the path of our project and we will make a single modification to it.
Our developed project now has data files and I'm going to assume the data files are the following:
01. text
01. json
And 3 audio files in wav format
In order to find these files integrated with the resulting executable file, we search inside the test.spec file for the datas element, and we will find it like this:
datas = [] so we modify it like this:
datas = [('01.txt', '.'),('01.json','.'),('.wav', '.')] “.” It means that the data files are located in the project folder. If these files or some of them are located in subfolders, then we substitute “.” with the name of the subfolder.
Thus, we have added all the data files of the project to test.spec, which we will convert into an executable file.
After completing the modification process, we save these modifications, close the test.spec file, go to the command prompt, and write the following
pyinstaller test. spec
After completing the conversion process successfully, we will find our executable file inside the dist folder in the path of our project, and all our data files will be integrated into that file, and our program will run smoothly, God willing.
Now we have the right to rejoice in our achievement, but we will discover something that will disturb our joy, which is the size of the executable file, which will be disturbingly large.
In this case, what should we do?
The answer to this question will be the focus of our third lesson, God willing.
3- Lesson 3: pyinstaller, the problem of large executable file size, reasons and solutions.
In our previous tutorial we converted our project into an executable file and it was annoyingly large.
Before talking about solving the problem of the large size of the executable file of our program, we will talk about the conversion process, how it is done.
I will talk here about the Python language, so that our program that we designed with Python can run on any computer that does not have Python. The pyinstaller copies the Python working environment, including the libraries that we used in our project, and includes all this environment inside the executable file in addition to our project files. That program since it contains all the work environment by which our program was designed.
It seems normal and logical, so where is the problem and what does this have to do with the large size of the executable file that we are looking for a solution to?
The problem is that when pyinstaller transfers the Python work environment and puts it in the executable file, it does not only put the libraries that the project needs, which we used to import in our program code, but also includes a number of other redundant libraries, and for accuracy and scientific honesty, I do not know on what basis pyinstaller chooses libraries other than Necessary to put it in the executable file, so the problem of the large size of the executable is caused by these libraries that our program does not need and includes pyinstaller for the executable file.
Solving the problem of large executable file size should address this point in particular.
But how do we force pyinstaller to include only the necessary libraries in the executable?
The method by which we will deal with the problem of the large size of the executable file will adopt this approach, i.e. force pyinstaller to include only the libraries necessary for the project to work, and these libraries are the ones we imported into our project and run as a Python file.
The solution is in the so-called “virtual environment”, and we can create a virtual working environment for each project.
Example:
Our executable file, which we got in our previous lesson, and which bothered us because of its large size, we will delete it, as we do not want it because it is not practical.
And we will return to our original project in Python format, and as a reminder, its name was test.py
We create a new folder named envs and this folder envs we will put the default environment for our current project and all our future projects.
After creating the envs virtual environments folder, we will create within it the first folder that will be in the name of this project, i.e. test.
We will assume that the path of the new folders is:
envs folder:
D:\programmation\envs
and test folder:
D:\programmation\envs\test
We can now create a virtual working environment like this:
At the command prompt we type:
Microsoft Windows [version 10.0.19044.1645] (c) Microsoft Corporation. All of our reserves.
C:\Windows\System32>python -m venv D:\programmation\envs\test
We press Enter and wait for the process of creating the virtual environment for the test project to finish. When this process is finished, the command prompt will return to its normal state like this:
C:\Windows\System32>
Now the default working environment for our project has been created.
We go to the test folder and find that it contains new items.
Our default work environment is present, but it is not activated. To activate it, we write the following at the command prompt:
C:\Windows\System32>D:\programmation\envs\test\Scripts\activate.bat
Then press Enter, and the command prompt looks like this:
(test) C:\Windows\System32>
We notice that the name of our default work environment is written at the beginning of the line and placed in parentheses, which means that we are now in our work environment and it is activated.
To verify the libraries in the default work environment of the test project, we write “
(test) C:\Windows\System32>pip list
After pressing Enter, we find in front of us a list of libraries and their versions installed in our work environment like this:
Package Version
pip 18.1
setuptools 40.6.2
You are using pip version 18.1, however version 22.1 is available.
You should consider upgrading via the 'python -m pip install –upgrade pip' command.
(test) C:\Windows\System32>
Now the field is ready to start our actual work and the first thing we have to do is install the libraries that our project needs, including pyinstaller, for example:
(test) C:\Windows\System32>pip install pyinstaller
We note that we treat this default work environment just as we deal with the normal work environment for Python.
After installing all the libraries that our test.py project needs, we try it and run it, we convert it into an executable file according to what was explained in our second lesson.
And the size of the executable file will be small and different from its size when we converted it in the traditional way.
As a reminder and confirmation, all work takes place inside this environment, which means that the command prompt is like this.
(test) C:\Windows\System32>
After completing our work, we close this virtual environment by disabling it with this command
(test) C:\Windows\System32>deactivate
Here we notice that we have returned to the normal mode of the command prompt like this:
C:\Windows\System32>
I hope that this method will succeed in reducing the size of your executable files. Peace, mercy and blessings of God be upon you.
Tunisia on 04/06/2022.
Saad atta says
I still have a problem. When I convert the file into an executable file, it works fine the first time, but when I close it and restart it again, it does not execute the commands.