Typically when making a program, I want users to have a familiar experience: a single .EXE file instead of some weird scripts that requires other programs to run. There are a few different ways of accomplishing this using python,. On this page I’ll explain the simplest way I’ve found to get this result, and how to debug if you run into problems with this method.
I wrote about using pySimeplGui for making dead simple GUI interfaces for python code before, and even have a simple example project,. Check those two articles out for that aspect.
As for creating the .EXE file, there are several packages that will allow you to create executables of your python scripts. The reason to do this is that you don’t want end users to have to install python (simplified usage of your app) or you don’t want anyone seeing the actual python code. The way they work is that they create a self-extracting zip file that has your code, all associated libraries (including DLLs), and most crucially, they have a small bootloader that is a python interpreter executable. There are several available such as cx_Freeze, bbFreeze, Py2App, Py2Exe, Pyinsaller, or you can create your own from scratch (See a chart comparing them here).
There are some problems with these tools. Several of these are not currently updated. Some don’t allow you to create a “One File” solution, meaning they require additional folders of files, etc. which in my view isn’t a true executable experience from the user perspective. For some reason, there’s a known issue with some of these which makes the resulting executable get incorrectly flagged as a virus by virus software. There are some ways around this however.
I’ve tried out a few of these and given my recent experience with PySimuleGui, and the features I wanted, I settled on pyinstaller.
PysimpleGUI has its own interface for pysintaller to simplify things called pysinstaller-exemaker.
For some reason, my resulting executables kept getting flagged with false virus warnings on windows. I tested in Virus Total which runs multiple virus scanners at once on your file. Sure enough, lots came back with false positives.
I found two ways to fix this problem. As with most things, there’s the easy way, and then there’s the right way.
The easy way requires specific versions of python and pyinstaller on your machine.
The other way to prevent false positive virus scans is to recompile pyinstaller’s bootloader from scratch. While I did this (and explain it below), it is non-trivial.
The Easy Way: Install Correct Versions:
The easiest way to not have the virus issue is to use the correct versions of pyhton and pyinstaller as described on a no-dead link on yuriss.com. This Completely solved the problem and is SOOO simple! Basically you just install the correct version of python (Version 3.7.4) from python.org. If getting the windows version, make sure it is the 64-bit version, other users seem to have had issues with the 32-bit version.
Once this is installed, you can use pip on the command line to install Pyinstaller 3.4:
pip install pyinstaller==3.4
The Right Way: Recompiling pyinstaller’s bootloader from scratch
This may be needed for future version of python or pyinstaller. Since I already went through the headache of figuring out how to do it, I’m documenting it here.
If using a version of pyinstaller that throws the virus warnings, you can’t just go to the folder PIP installed it to and compile it there. That’d be too easy of course! (This took me a long time to figure out)… If you try to do that, it’ll fail with cryptic errors. The reason is that the file path is too long.
To overcome this, you have to perform the following steps:
- Clone pyinstaller’s source to a folder in your C drive, then
- Rebuild the bootloaders,
- Install pyinstaller with pip,
- Overwrite your pip installation with your newly built bootloader files…
Duh, obvious right?! (omgwtfwhyisthisalwayssohard!?)
Details for the steps are below:
Step 1: Open a powershell as admin and go to C:\\
cd c:\\
Download the source of pyinstaller. This will create its own folder for pyinstall:
>pre>git clone https://github.com/pyinstaller/pyinstaller
Step 2: Cd into the bootloader’s build folder:
cd .\\PyInstaller\\bootloader\\
Then run the script to reinstall. If you get an error telling you there’s no such thing as ./waf then you are in the wrong folder.
python ./waf all
Step 3: Once this is done, you can go back to vscode or wherever and install pyinstaller from pip
pip install pyinstaller
Step 4: Then navigate to python’s site packages, and copy the newly built bootloaders into the appropriate place. NOTE: PyInstaller is case sensitive here, so be careful. We’ll first make a backup of the original bootloader folder:
mv C:\\Users\\ALaptop\\AppData\\Roaming\\Python\\Python38\\site-packages\\PyInstaller C:\\Users\\ALaptop\\AppData\\Roaming\\Python\\Python38\\site-packages\\PyInstaller.bak
Then copy in our freshly compiled bootloader:
mv C:\\PyInstaller\\bootloader C:\\Users\\ALaptop\\AppData\\Roaming\\Python\\Python38\\site-packages\\PyInstaller
Note: Now to use pysimplegui-exemaker, you must edit your computer’s %PATH environment variable to locate pyinstall. Click the windows key on the keyboard, type “environment” and click to open the first suggestion. This window will pop up that has a button towards the bottom named “Environment Variables” that you must click. In the top window pane, find “path” or “PATH” and doubleclick that line to edit it. You’ll now be able to enter a new value. You want to add the path to where pip installs its scripts. In my case, I pasted in the following:
C:\\Users\\ALaptop\\AppData\\Roaming\\Python\\Python38\\Scripts
Then test it out:
python -m pysimplegui-exemaker.pysimplegui-exemaker #<--If you use pysimplegui's exemaker (NOT a typo, must exactly this way) OR
pyinstall --onefile ./myProgram.py #<-- If you just use pyinstaller directly
Tada! No more virus warnings!!!!
Check out my other post about how to replace the default python icon in your executable files.