[How To] Create your own Python Executable! (.exe file) Full In-Depth Tutorial
You will learn how to successfully bundle a Python project into a standalone *.EXE file (with dependencies and external resources!)
To start off, we need to download some things ourselves
1. We need to install PyInstaller. This is a simple Python application that will bundle all of your project files into the standalone EXE.
All you need to do is run a simple command (for this tutorial I will be using Python 3)
pip3 install pyinstaller
You need to download the Windows 10 SDK (or a signing tool based on your OS) Just head over to this link: https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk
And click the big ‘Download Installer’ button!
Now we let it install (let it install in the background, you can continue with the tutorial)
Ok, now for creating the executable
This part is pretty simple. Just running a few commands (kind of)
To start off with, you need to open a command prompt in your project directory.
We will now set up the specfile for your project. This makes a specification file which we can customize to our liking.
There is a specific structure for your folder before we run the command has to be like this:
| |---project dependencies
Mine is like this:
Assuming that step went fine, the command you want to run is this:
pyi-makespec script.py -n my_specfile --onefile
The ‘-n’ option specifies the name of your specfile. The ‘ — onefile’ tells the program to bundle your project as a single executable.
Ignore where it says ‘now run pyinstaller.py’, we have a bit of customization to do.
Open up the my_specfile.spec in a text editor of your choice.
Make it look like this:
a = Analysis(['script.py'],
Where pathex is the path of your project, and a is your script.py.
The datas=get_resources() is what needs to be added
Also add (just under block_cipher = none) this:
data_files = 
for file_name in os.listdir('resources'):
data_file.append((os.path.join('resources', file_name), 'resources'))
(please note the above code is written with improper spacing. Please fix this when coping over)
That is all we need to do for the my_specfile.spec so you can go ahead and close that.
Now run this command in the terminal:
That should produce a whole lot of code and create your .exe file.
Now, signing the *.EXE
Now you might be able to run this EXE file on your computer, but if you try to run it on another it will tell you ‘Unknown Publisher’ and it won’t run.
(during these next few commands, if anyone of them asks for a password just select NONE)
To fix this we need to sign the EXE file. First (in the same directory) run this command (EXACTLY, IT IS CASE SENSITIVE):
"C:\Program Files (x86)\Windows Kits\10\bin\x64\makecert.exe" /r /h 0 /eku "126.96.36.199.188.8.131.52.3,184.108.40.206.4.1.3220.127.116.11" /e 12/12/2050 /sv MyKey.pvk MyKey.cer
You just created a certificate for yourself! Now we need to convert these files to a .PFX.
Run this command EXACTLY:
"C:\Program Files (x86)\Windows Kits\10\bin\x64\pvk2pfx.exe" /pvk MyKey.pvk /spc MyKey.cer /pfx MyKey.pfx
PHEW, The hard part out of the way!
Now, all we need to do is run one simple command:
"C:\Program Files (x86)\Windows Kits\10\bin\x64\signtool.exe" sign /a /f MyKey.pfx PythonProgram.exe
You should now have a fully functioning python executable.