Python and PyInstaller — Introduction and Troubleshooting

  • Distributing your workflow to non-technical users
  • Obfuscating your code

Introduction to PyInstaller

pip install pyinstaller
pyinstaller [python_file]
pyinstaller [python_file] --onefile

PyInstaller Spec File

pyi-makespec [python_file]
  • Line 5: The filename of the Python script being compiled
  • Line 6: The directory of the Python script being compiled
  • Line 7–8: Additional binary and non-binary files. Binary files are often .dll files. You can try manually linking as mentioned above or link them here. Additional non-binary files can include pictures, database files, or others. If you get errors about missing files you should include them here.
  • Line 9: Additional imports that may be required by the compiler. Compiled software will require additional library imports compared to a non-compiled script (I will go over this later below). You should put the additional libraries here. The input is a list with each item in the list containing the name of the library as a string. For example: [‘numpy’, ‘rasterio’, ‘fiona’]
  • Line 10–11: If a library requires specific environmental variables they can be called upon here using a runtime hook. These are small scripts that manipulate the environment before your main script runs, effectively providing additional top-level code to your script. I often use them to manipulate the environment parameters using something like: os.environ[‘PARAM_NAME’] . There is more info later in this tutorial regarding this section.
pyinstaller your-spec-file.spec

Planning and Testing for PyInstaller

General test workflow

Troubleshooting the ModuleNotFoundError

cd "your/project/folder/here"
pyinstaller import_tests.py
Output of PyInstaller while it is compiling our script
New files and folders in the project folder after compilation
cd "your/dist/folder"
import_tests.exe
ModuleNotFoundError when importing Rasterio. Traceback reveals rasterio._shim is missing
pyinstaller import_tests.py
The missing import is highlighted in red
New line of code seen in line 3

Setting Environment Parameters with Runtime Hooks

Final Thoughts

--

--

--

Researcher working on geospatial sciences and general programming

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

JW Player — The Commercial Video Player Overview, Part 4

Pulsar Flink Connector 2.5.0

How to Write Automation Tests for Feature Flags with Cypress.io and Selenium

Unveiling the new Logo and Design of Dripfeedbot

Community Priorities — Q2 2022

Big data mangement

C# Variables

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Panji Brotoisworo

Panji Brotoisworo

Researcher working on geospatial sciences and general programming

More from Medium

Beginners Python Series — Datatypes

The Python Series #4 — While Loop and General Terminologies

Reversing part of a string in Python

The Minion Game with Python