How to create self contained executables in Python
This is a very quick guide on how to create self contained Python executables (for all platforms). First of all install PyInstaller (I am using pip3 because I work with Python 3.x):
sudo pip3 install pyinstaller
Now install upx for a better compression:
sudo apt-get install upx
Now you are ready to create your self-contained executable:
pyinstaller \
--onefile \
--noconfirm \
--noconsole \
--clean \
--log-level=WARN \
--key=MySuperSecretPassword \
--strip \
myscript.py
Resulting executable will be placed inside dist folder and it will be called myscript. This is what each parameter does:
--onefileallows to create a single self contained binary.--noconfirmreplaces output directory without asking for confirmation.--noconsoleshould be used in GUI application with no console.--consoleshould be used otherwise.--cleancleans PyInstaller cache and remove temporary files.--log-level=WARNshows only warnings and errors during build.--key=yourkeyuses the given key to encrypt the Python bytecode (yes it’s secure!).--stripremoves debug information to executable and shared libraries.
For windows, if you want to add also the icon to the resulting exe file you can add this additional parameter:
--icon-file=myapplication.ico
The application have only to be recompiled on every platform you want to release your application for. I know, the resulting binary will be a little heavy (~24MB for a PySide GUI application on Linux) but we have to consider that it contains the interpreter itself and all the needed libraries!!!