USING PYINSTALLER...
When you are creating an executable with pyinstaller, then there are some things you have to consider. It is recommended to use Auto-py-to-exe which is an easy interface for pyinstaller.
One-Dir or One-File?
First add your main python program in the script location option.
Then you will have to choose between two options: One Directory and One File.
Packaging with one-directory
This mode will give a folder where you will get lots of files including the exe. The main benefit of using one-dir is that you will get a faster loading executable with all the folders in one place.
But the problem is that the folder looks ugly with so many files, you may have to use some exe-installer/setup creater.
Some good setup creators are: installForge and Inno-Setup
(If you are using this mode, then skip the one-file part)
Packaging with one-file:
With this mode, you will get a single executable file. When you run that executable, a temp folder (named as _MEIPASS...), will be created in an OS defined temp location where it will extract all the necessary files that we used to get with the one-dir mode. This unpacking is done every time when you open that executable which affects the startup time. This is how the one-file mode works.
Packing external files with one-file mode:
Generally, if you are using any external assets like images, sub-folders or any other files, then you will need to keep that asset outside the one-exe file.
But if you want to pack those assets/folders inside that one-exe file, then you will have to do a little tweak in your program.
To do this, you have to first add those assets/folders under the additional files option of pyinstaller.
Then you will have to change the search method for all assets in your program:
# Just add this search function:
import sys, os
def resource(relative_path):
base_path = getattr(
sys,
'_MEIPASS',
os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
# Then use this function to find the asset, eg: resource("my_file")
If you are importing an asset like this:
image = "CustomTkinter_logo.png"
Then change it to this:
image = resource("CustomTkinter_logo.png")
Note: Make sure that the file paths are relative, avoid uses of slash, instead use os.path.join method.
We are doing this because the one-file exe unpacks its files in a separate temp directory when it is executed. So, you have to use this method to search the packed files within that hidden temp folder.
> If you want to reduce the size of one-file exe, then here is a pro-tip
Another pro tip for one-file mode:
Sometimes you may have large libraries and modules included in the one-exe file which results in slower startup time.
In that case you can exclude some module folders outside the main exe file with some tweaks:
Here is how you can do it:
Make a folder Modules in the same directory where main program is present.
Copy and paste all the heavy modules inside that folder.
Add this search path function inside your main program:
# add this code in the top (before imports)
import sys, os
if getattr(sys, 'frozen', False):
app_path = os.path.join(os.path.dirname(sys.executable), "Modules")
sys.path.append(app_path) # search this path for modules
Under advanced pyinstaller option --exclude-module, write the names of all those modules you excluded in that module folder.
This will reduce the startup time and size of the executable
> Another pro tip if you want to change the default temp dir location
Pro-Tip for one-file mode:
Generally the temp folder for one file mode (named like MEIxxxxxx) is extracted in an OS defined temp location. But sometimes, if your app crashes or closed forcefully, then the temp dir is not removed automatically which can lead to c-drive storage issues.
Moreover, many basic users don't know the default os temp location.
But there is an option for the one-file mode in which the temp folder spawn location can be changed.
Under Advanced tab there is an option called --runtime-tmpdir, you can just write "temp" in that field.
Now, the temp folder will be extracted in the same directory where the exe is present (inside a new 'temp' dir).
In this case, users can easily find and delete that temp folder if the app crashes. So, it is just good for the user side specially if your app is in experimental stage.
Disadvantage: The app will not open if the user runs the exe inside any read-only location having no access to write.
Note: This feature only works for Windows.
Console Window
Under this section, you will get two options: console-based and window-based.
Console-Based: You will get a console window running in the background with your main app.
However there is also new option to hide/minimize the console automatically after startup called hide-console
Window-Based: You will only get the app window. (recommended for most applications)
Icon
Under the icon section, you can set the icon for your app. (Just add your .ico file)
Note that this icon will not be shown inside the application titlebar.
To pack the custom icons read the next heading.
Additional Files and Folders
You can add all other assets/folders/modules accordingly with pyinstaller.
For some module like customtkinter, You will have to add the module folder because pyinstaller doesn't automatically include files like .json or .png from the library.
You can find the install location of the customtkinter library with the following command:
pip show customtkinter
A location will be shown in the details, for example: c:\users\<user_name>\appdata\local\programs\python\python310\lib\site-packages, where you will find the customtkinter package.
Similarly, for any other module import error, try to pack them separately like this.
For easier access, it is recommended to keep a copy of the module folders like customtkinter in the same directory where your python program is present.
Output
There are many other advanced options you can tweak if you search mode in the advanced tab, like adding a splash screen image and hooks.
Otherwise just move to the Settings section and choose your Output Directory where the executable will be created.
Then just click on Convert!
That's all you need to know about packaging your customtkinter app to an executable file with pyinstaller.
> Dealing with Anti-Virus False Positives
Common Issues with PyInstaller Executables One of the most frequent challenges with executables created using PyInstaller is receiving false positives from antivirus software. Not all antivirus programs flag them—only a few, seemingly at random. To address these issues, here are some solutions:
Buy a legitimate code signing certificate.
Distribute your application via the Microsoft Store (if applicable).
Experiment with standard packages—try adding or removing packages (like numpy)
Test with free and widely-used antivirus programs. Identify which ones generate false positives, then visit their forums and report the issue related to your app.
Use VirusTotal for testing: If the VirusTotal score is below 10, the file is considered within normal limits.
Update the pyinstaller and python versions
Provide clear explanations to your customers about the warnings.
Among these solutions, obtaining a code signing certificate is the most effective option. While it requires an initial investment, it eliminates the "unknown developer" tag from your app and significantly enhances its credibility.
In most cases, false positives diminish over time if the app is widely downloaded and has established reputation among users.
> Creating MacOS Universal Apps (both Intel and Apple Silicon)
If you are on MacOS, there is another guide highly remommended fo you: Universall apps in Pyinstaller
> Common doubts [FAQ]
Q. Can we make executable for other systems in Windows?
A. No, pyinstaller in windows will only generate .exe files, for MacOS, you will need to use pyinstaller in mac hardware, same goes with linux. However, you can use virtualbox if you have limited devices.
Q. What to do if there is a specific bug in Pyinstaller?
A. Report the bug in Pyinstaller github repository
Q. Will the exe work in Windows 7, Windows 8 or Windows 10?
A. PyInstaller should work on Windows 7 or newer
Qcx
Q. Will it work in Mac OSX?
A. Yes, macOS 10.15 (Catalina) or newer are supported.W `.
FULL TUTORIAL VIDEO (MUST WATCH)
THAT'S ALL,
by AKASCAPE