Create Beam Cruncher Plus Executable - crunchtec/Beam-Cruncher-Wiki GitHub Wiki

This wiki page describes how to create an executable for the Beam Cruncher Plus system (client only, cameras connected via USB) for either Linux or Windows.

Create a Beam Cruncher Plus Executable for Linux

Create Virtual Environment

Start by creating a virtual environment so that the packages/libraries needed for the Beam Cruncher Plus executable are kept separate from the other packages/libraries on your computer. Follow the directions here to create and activate a new virtual environment: https://realpython.com/python-virtual-environments-a-primer/#using-virtual-environments

Install Required Packages (Dependencies)

With the virtual environment you just created active, install all required packages, starting with the version of Python you wish to build the executable with. Then, use the command "pip[major Python rev #].[minor Python rev #] install [package name]" to install the required packages to the correct version of Python's library (i.e. your virtual environment may have more than one version of Python installed, each with their own set of installed packages).

Run your application with the desired Python revision and test all functionality.

Install Pyinstaller and Create a SPEC File

Install the "pyinstaller" package by using the same "pip install" command you used in the last step. Pyinstaller is used to build the executable from your Python script and is cross-platform. However, Pyinstaller will only be able to build an installer for the operating system you are running it on, that is, a Pyinstaller build run on Windows will produce a Windows .exe file, etc.

Note: SPEC files used for previous releases are stored on the CrunchFTP server @: \PiCrunchFTP\CrunchFTP\Camera project\Pyinstaller SPEC files

To create a new SPEC file, type the command "pyi-makespec --onedir BeamCruncher.py". This will generate a SPEC file for the BeamCruncher.py Python script. Open the BeamCruncher.spec file created in your favorite text editor.

Add Basler PyPylon/Pylon dlls to Spec File

The Beam Cruncher Plus uses USB cameras that require pypylon/pylon so's (shared object files - essentially dlls for Linux). Include these in your build by adding the following lines to the top of your spec file (just after any "utf-8" declaration, if there):

import pypylon
import pathlib
pypylon_dir = pathlib.Path(pypylon.__file__).parent
pylon_dlls = [(str(dll), '.') for dll in pypylon_dir.glob('*.so')]

then, set the "binaries" attribute in the Analysis object constructor to "pylon_dlls" - i.e. the list of so's we generated with the above lines of code. Also, add all image files needed by the application to the Analysis "datas" attribute in the format shown below. These changes should look like:

a = Analysis(['BeamCruncher.py'],
             pathex=['/home/crunch/BeamCrunch-StandaloneClient-Python3Env/env/lib/python3.6/site-packages/numpy/core/', '/home/crunch/BeamCrunch-StandaloneClient-Python3Env/env/lib/python3.6/site-packages/', '/home/crunch/BeamCruncher-StandaloneClient/Beam-Cruncher-Application'],
             binaries=pylon_dlls,
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)

a.datas += [('crunch_icon.png','resources/crunch_icon.png','DATA')]
a.datas += [('crunch_logo.png','resources/crunch_logo.png','DATA')]
a.datas += [('splash_screen.png','resources/splash_screen.png','DATA')]

Note: I had issues with Pyinstaller finding the numpy.core.multiarray package during my build. If you have an error related to numpy.core while running your generated binary executable, add the paths in the spec file above to the "pathex" attribute of the Analysis constructor (i.e. just as shown above).

Add Icon Location to Spec File

By default, the icon parameter in the EXE function generated by the "pyi-makespec" command is set to "None" (or may not be present at all). Change this to read "icon='[location of .ico file]'" (do not include outer double-quotes) and use forward slashes for the path. This will set the icon for your executable. Make sure that the "debug" and "console" parameters are both set to "False". Your EXE constructor should like something like:

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='BeamCruncher',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False,
          icon='resources/crunch_icon.ico')

Build the Beam Cruncher Plus Executable

To build the Beam Cruncher Plus executable, run the command "pyinstaller BeamCruncher.spec" (Note: use your spec file you just created, NOT THE BeamCruncher.py SCRIPT! Otherwise, pyinstaller will regenerate the generic .spec script and replace all of your hard work!)

Create a Beam Cruncher Plus Executable for Windows

Create Virtual Environment

Start by creating a virtual environment so that the packages/libraries needed for the Beam Cruncher Plus executable are kept separate from the other packages/libraries on your computer. Follow the directions here to create and activate a new virtual environment: https://realpython.com/python-virtual-environments-a-primer/#using-virtual-environments

Install Required Packages (Dependencies)

With the virtual environment you just created active, install all required packages, starting with the version of Python you wish to build the executable with. Then, use the command "pip[major Python rev #].[minor Python rev #] install [package name]" to install the required packages to the correct version of Python's library (i.e. your virtual environment may have more than one version of Python installed, each with their own set of installed packages).

Run your application with the desired Python revision and test all functionality.

Install Pyinstaller and Create a SPEC File

Install the "pyinstaller" package by using the same "pip install" command you used in the last step. Pyinstaller is used to build the executable from your Python script and is cross-platform. However, Pyinstaller will only be able to build an installer for the operating system you are running it on, that is, a Pyinstaller build run on Windows will produce a Windows .exe file, etc.

To create a new SPEC file, type the command "pyi-makespec --onedir BeamCruncher.py". This will generate a SPEC file for the BeamCruncher.py Python script. Open the BeamCruncher.spec file created in your favorite text editor.

Add Basler PyPylon/Pylon dlls to Spec File

The Beam Cruncher Plus uses USB cameras that require pypylon/pylon dlls . Include these in your build by adding the following lines to the top of your spec file (just after any "utf-8" declaration, if there):

import pypylon
import pathlib
pypylon_dir = pathlib.Path(pypylon.__file__).parent
dlls = [(str(dll), '.') for dll in pypylon_dir.glob('*.dll')]
dlls.append(('C:\\Users\\Luke\\Code\\BCPlus_env_Py36\\Lib\\site-packages\\cv2\\opencv_videoio_ffmpeg411_64.dll','.'))

then, set the "binaries" attribute in the Analysis object constructor to "pylon_dlls" - i.e. the list of dlls we generated with the above lines of code. Also, add all image files needed by the application to the Analysis "datas" attribute in the format shown below. These changes should look like:

a = Analysis(['BeamCruncher.py'],
             pathex=['C:\\Users\\Luke\\Code\\BeamCruncher-StandaloneClient\\Beam-Cruncher-Application'],
             binaries=dlls,
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)

a.datas += [('crunch_icon.png','resources/crunch_icon.png','DATA')]
a.datas += [('crunch_logo.png','resources/crunch_logo.png','DATA')]
a.datas += [('splash_screen.png','resources/splash_screen.png','DATA')]

Add Icon Location to Spec File

By default, the icon parameter in the EXE function generated by the "pyi-makespec" command is set to "None" (or may not be present at all). Change this to read "icon='[location of .ico file]'" (do not include outer double-quotes) and use forward slashes for the path. This will set the icon for your executable. Make sure that the "debug" and "console" parameters are both set to "False". Your EXE constructor should like something like:

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='BeamCruncher',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False,
          icon='resources/crunch_icon.ico')

Build the Beam Cruncher Plus Executable

To build the Beam Cruncher Plus executable, run the command "pyinstaller BeamCruncher.spec" (Note: use your spec file you just created, NOT THE BeamCruncher.py SCRIPT! Otherwise, pyinstaller will regenerate the generic .spec script and replace all of your hard work!)

If successfully built, the executable can be found in the newly created "dist" directory.

Final Notes

If your Pyinstaller build is having issues finding any of the modules required, help it out by adding those modules to the "pathex" parameter in the Analysis object constructor of the spec file. Also, for debugging, set the "debug" and "console" parameters in the EXE constructor to "True".

Lastly, be sure to place your SPEC file used in the CrunchFTP directory: \PiCrunchFTP\CrunchFTP\Camera project\Pyinstaller SPEC files.