[How To] Create your own Python Executable! (.exe file) Full In-Depth Tutorial

in #programming6 years ago

Hi there! Welcome to my tutorial on how to create a *.EXE file from your Python Application.

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

First:

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

Next:


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 the EXE.

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.
Now we run the 'pyi-makespec' command. This makes a' pyinstaller' specification file which we can customize to our liking.
Assume your project structure is like this:
code_directory |---resources |   |---project_dependencies |---script.py
Mine is like this:

Anyways we have to run this command:

pyi-makespec script.py -n my_specfile --onefile

Ignore that last line, we have a bit of customization to do.

Open up the my_specfile.spec in a text editor of your choice.
It should look like this

Now just add this:
a = Analysis(['script.py'],  pathex=['C:\PythonProgram'],  binaries=[],  datas=get_resources(),
The datas=get_resources() is what needs to be added
Also add (just under block_cipher = none) this:
def get_resources():  data_files = []  for file_name in os.listdir('resources'):   data_files.append((os.path.join('resources', file_name), 'resources'))  return data_files
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:
pyinstaller my_specfile.spec
That should produce a whole lot of code

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 any one 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 "1.3.6.1.5.5.7.3.3,1.3.6.1.4.1.311.10.3.13" /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

There you go, your *.EXE is successfully signed and is fully able to run on any system! :)

Thanks for reading this post, if you enjoyed please upvote and follow me!!!)

Sort:  

Congratulations @stolentissue! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!