Python PyInstaller on Linux
Table of contents:
- Install the PyInstaller
- Experiment: creating the installation
- What will PyInstaller do?
- When PyInstaller output is not a single file
- Command line arguments
Thanks to PyInstaller it is now easy to create the installation for a Python based project you distribute to: Linux, Windows or MacOS. In here I will examine the PyInstaller on Linux.
When on Linux, you cannot create the final executable for Windows or MacOS, that would be the PyInstaller limitation.
This is why for each OS (Operating System) you should build separate distributable executable.
In other words, PyInstaller doesn’t have the platform switch, like Python Kivy or C# Unity tools that are cross platform.
Install the PyInstaller
First step would be to download PyInstaller from PyPi, but it is even easier to install it using pip:
pip install pyinstaller
If you are not sure you have the latest PyInstaller version already you can try to install and upgrade:
pip install --upgrade pyinstaller
From now on, you can call pyinstaller
from the command line.
Experiment: creating the installation
Let’s make our fingers dirty with some code. Let’s create a program you can distribute to any Linux platform even if Python is not installed. In other words we will create the Linux standalone executable as the output of this experiment.
First we create a folder called /content to hold our experiment. In there we create a file called getdatetime.py:
# save as getdatetime.py
import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))
Check here how to work with Python date and time in detail.
Assuming we installed the PyInstaller already, we are now ready to create the one-file standalone executable.
pyinstaller --onefile getdatetime.py
If we list the /content folder, we will get the following output. Wow!
If we now execute the file
command to check the file type for the getdatetime:
file /content/dist/getdatetime
We get the output this is ELF Linux executable:
/content/dist/getdatetime: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 2.6.32, BuildID[sha1]=294d1f19a085a730da19a6c55788ec08c2187039, stripped
To run the executable we simple type from the command line:
/content/dist/getdatetime
Output:
Date and time is:
2020-02-22 11:35:15
This is how simple it is; to create independent executable you may run on any Linux, with or without Python installed.
What will PyInstaller do?
We had originally just a single detdatetime.py file, and after the PyInstaller command we have several files and folders more:
build/
dist/
getdatetime.py
getdatetime.spec
__pycache__/
build/
folder is where PyInstaller prepares the distribution executable and stores that to dist/
folder. The getdatetime.spec
is a specification file that PyInstaller creates the first time you run it.
You can edit the getdatetime.spec
file and name your output executable other way.
When PyInstaller output is not a single file
There is another way. You can run PyInstaller with this command:
pyinstaller --onedir getdatetime.py
This time we use --onedir
switch. Inside the dist folder we now have different output. Recall that with the --onefile
option we had just a single file output.
There are many
.so
files inside our dist folder. Files with.so
extension are Shared Objects files. In Windows these would be.dll
s. Under Linux you can get some feedback on.so
files usingldd
command.
To run the final executable and to get the output we should call from the command line:
/content/dist/getdatetime/getdatetime
Output:
Date and time is:
2020-02-22 12:02:15
Command line arguments
Basic switches:
Output to generate:
…
tags: install - installer - linux & category: python