Skip to content

Caveat Usage with pyinstaller

Tyler Alden Gubala edited this page Nov 8, 2019 · 2 revisions

Usage with PyInstaller

pyinstaller is a package capable of generating an executable file from python code.

It does so by bundling dependencies and grabbing any binary code or libraries that the python file its bundling might need.

It's not perfect at finding these complexities, unfortunately. Fortunately, it is relatively straightforward to remedy any issue and get PyInstaller working with bpy.

The most common issue to have is not bundling the Blender scripts properly. To avoid this, we simply specify the scripts directory to be installed alongside the executable, by calling it "data" using the pyinstaller --add-data command line option, like below.

In the terminal:

mkdir test_bpy_pyinstaller
cd test_bpy_pyinstaller

py -3.6-64 -m venv venv
venv\Scripts\activate
py -m pip install bpy
py -m pip install pyinstaller

In a new Python file in your test_bpy_pyinstaller folder, called test.py:

#!/usr/bin/python
# -*- coding: utf-8 -*-
try:
    import bpy
except:
    print("Failed to import bpy")
else:
    print("Successfully imported bpy!")

Back in the terminal:

pyinstaller --add-data="venv/Scripts/2.79;2.79" test.py

[  PyInstaller does its thing...  ]

dist\test\test

[  Should see "Successfully imported bpy!"  ]

Additional reading

See #28