Using PyInstaller to create an executable file from a Python script

If you wish to convert Python scripts to executable files that you can run on systems where Python or all of the needed dependencies for the script are not installed, one program that is available for Linux, Mac OS X, Solaris, AIX, or Microsoft Windows systems is PyInstaller. If you have the pip package manager installed, you can install PyInstaller by running the command below from the root account.

pip install pyinstaller

To then create an executable file that will run on other sytems with that same operating system, e.g., you can create an executable file on one Linux system that will run on another Linux system or create an .exe file on a Microsoft Windows system that can be ported to another Windows system, you can issue the command pyinstaller E.g.:

$ pyinstaller --onefile
110 INFO: PyInstaller: 3.3.1
111 INFO: Python: 2.7.5
112 INFO: Platform: Linux-3.10.0-229.7.2.el7.x86_64-x86_64-with-centos-7.1.1503-Core
113 INFO: wrote /home/jdoe/temp/findTextBlock.spec
122 INFO: UPX is not available.
125 INFO: Extending PYTHONPATH with paths
126 INFO: checking Analysis
126 INFO: Building Analysis because out00-Analysis.toc is non existent
126 INFO: Initializing module dependency graph...
132 INFO: Initializing module graph hooks...
303 INFO: running Analysis out00-Analysis.toc
334 INFO: Caching module hooks...
346 INFO: Analyzing /home/jdoe/temp/
4930 INFO: Loading module hooks...
4934 INFO: Loading module hook ""...
6857 INFO: Looking for ctypes DLLs
6858 INFO: Analyzing run-time hooks ...
6877 INFO: Looking for dynamic libraries
7639 INFO: Looking for eggs
7639 INFO: Using Python library /lib64/
7645 INFO: Warnings written to /home/jdoe/temp/build/findTextBlock/warnfindTextBlock.txt
7700 INFO: Graph cross-reference written to /home/jdoe/temp/build/findTextBlock/xref-findTextBlock.html
7845 INFO: checking PYZ
7846 INFO: Building PYZ because out00-PYZ.toc is non existent
7846 INFO: Building PYZ (ZlibArchive) /home/jdoe/temp/build/findTextBlock/out00-PYZ.pyz
8338 INFO: Building PYZ (ZlibArchive) /home/jdoe/temp/build/findTextBlock/out00-PYZ.pyz completed successfully.
8454 INFO: checking PKG
8454 INFO: Building PKG because out00-PKG.toc is non existent
8454 INFO: Building PKG (CArchive) out00-PKG.pkg
14083 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
14151 INFO: Bootloader /usr/lib/python2.7/site-packages/PyInstaller/bootloader/Linux-64bit/run
14152 INFO: checking EXE
14152 INFO: Building EXE because out00-EXE.toc is non existent
14152 INFO: Building EXE from out00-EXE.toc
14153 INFO: Appending archive to ELF section in EXE /home/jdoe/temp/dist/findTextBlock
14191 INFO: Building EXE from out00-EXE.toc completed successfully.

When the process is completed, you will see two directories, build and dist, and a .spec file in the directory from which you ran the command. The .spec file contains all of the options, if any, used to run PyInstaller and can be used for future builds in lieu of entering those options again. The dist directory will contain the executable file that you can then transfer to another system with the same or similar operating system.

$ ls
build  dist  findTextBlock.spec
$ ls -lgh dist
total 4.5M
-rwxr-xr-x. 1 jim 4.5M Feb  3 19:07 findTextBlock
$ file dist/findTextBlock
dist/findTextBlock: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamic
ally linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0xd02fb9fdfc
9238de0c227621eef89466a34f1de6, stripped

In the build directory, you will see files like the following ones:

$ ls build/findTextBlock
out00-Analysis.toc  out00-PKG.pkg  out00-PYZ.pyz  warnfindTextBlock.txt
out00-EXE.toc       out00-PKG.toc  out00-PYZ.toc  xref-findTextBlock.html

The executable file in the dist directory is the only one you need transfer to the other system in order to run the program. E.g., in this case I would only need transfer the findTextBlock file from the dist subdirectory to the other system. The file was produced from a findTextBlock Python script that finds a block of text in a text file given a starting string and an ending string upon which to search.

Related articles:

  1. Installing pip to install Python packages on a CentOS system
  2. Find a block of text in a file