Guide
install
| 1 | pip3 install pywin32 pyinstaller | 
pyinstall -F demo.py error
pyinstaller AttributeError: 'str' object has no attribute 'items'
solution:
| 1 | pip3 install --upgrade setuptools | 
usage
| 1 | pyinstaller -h | 
params:
- General Options - -y, –noconfirm 
 Replace output directory (default: SPECPATH/dist/SPECNAME)
 without asking for confirmation
 –upx-dir UPX_DIR
 Path to UPX utility (default: search the execution path)
 –clean
 Clean PyInstaller cache and remove temporary files before building.
 –log-level LEVEL
 Amount of detail in build-time console messages. LEVEL may be one of
 TRACE, DEBUG, INFO, WARN, ERROR, CRITICAL (default: INFO).
- What to generate - -D, --onedir Create a one-folder bundle containing an executable (default) -F, --onefile Create a one-file bundled executable.
- What to bundle, where to search - --add-data <SRC;DEST or SRC:DEST> This option can be used multiple times. --add-binary <SRC;DEST or SRC:DEST> This option can be used multiple times. -p DIR, --paths DIR A path to search for imports (like using PYTHONPATH). Multiple paths are allowed, separated by ‘:’, or use this option multiple times --hidden-import MODULENAME, --hiddenimport MODULENAME Name an import not visible in the code of the script(s). This option can be used multiple times.
- Windows and Mac OS X specific options - -c, --console, --nowindowed Open a console window for standard i/o (default) -w, --windowed, --noconsole Windows and Mac OS X: do not provide a console window for standard i/o
Example
use pyd
path related source code
| 1 | sys.path.append('./sdk/superdog/') | 
pyinstaller commands
| 1 | pyinstaller -y -D --path="sdk/superdog" demo.py | 
generate build and dist folder, plus demo.spec
output
78 INFO: Extending PYTHONPATH with paths
['E:\\git\\python\\helloworld',
 'E:\\git\\python\\helloworld\\sdk\\superdog',
 'E:\\git\\python\\helloworld']
demo.spec
| 1 | # -*- mode: python -*- | 
view build/demo/xref-demo.html
superdog found.
Tips:
if we usepyinstaller -y -D demo.pydon’t include--path="sdk/superdog",
package will be missing, and error occur when we run executable.
run executable
| 1 | cd dist/demo | 
all related dlls have been copied to
dist/demo/folder,
eg.cublas64_80.dll,curand64_80.dll,cudart64_80.dll,cudnn64_6.dll
use ctype DLLs
path related source code
| 1 | sys.path.append('./sdk/superdog/') | 
CDLL related source code
| 1 | lib = CDLL("./sdk/detect/cuda80_detect_cpp_dll.dll", RTLD_GLOBAL) | 
pyinstaller commands
| 1 | pyinstaller -y -D --path="sdk/superdog;sdk/detect" demo.py | 
warning
93 INFO: Extending PYTHONPATH with paths
['E:\\git\\python\\helloworld',
 'E:\\git\\python\\helloworld\\sdk\\superdog',
 'E:\\git\\python\\helloworld\\sdk\\detect',
 'E:\\git\\python\\helloworld']
  ...
WARNING: Ignoring ./sdk/detect/cuda80_detect_cpp_dll.dll imported
    from E:\git\python\helloworld\sdk\detect\detect.py -
    ctypes imports are only supported using bare filenames
fix
| 1 | #lib = CDLL("./sdk/detect/cuda80_detect_cpp_dll.dll", RTLD_GLOBAL) | 
Tips: for CDLL with pyinstaller, we must use bare filenames in python source.
see here
now we run pyintaller again
| 1 | pyinstaller -y -D --path="sdk/superdog;sdk/detect" demo.py | 
view build/demo/xref-demo.html
superdog and detect.
run executable
| 1 | cd dist/demo | 
all related dlls have been copied to
dist/demo/folder,
eg.cublas64_80.dll,curand64_80.dll,cudart64_80.dll,cudnn64_6.dll
andcuda80_detect_cpp_dll.dll
Version
get version
| 1 | python grab_version.py "/path/to/xxx.exe" | 
C:\Users\zunli\AppData\Local\Programs\Python\Python35\Lib\site-packages
PyInstaller\utils\cliutils\grab_version.py

file_version_info.txt
| 1 | # UTF-8 | 
set version
| 1 | python pyinstaller.py \ | 
Advanced
Running PyInstaller with Python optimizations
Tips: be carefull with optimizations.
PyInstaller can be run with Python optimization flags (-O or -OO)
by executing it as a Python module, rather than using the pyinstaller command:
| 1 | python -O -m PyInstaller -y -D --path="sdk/superdog" demo.py | 
Or, by explicitly setting the PYTHONOPTIMIZE environment variable to a non-zero value:
| 1 | # Unix | 
only import what you need
replace import os with from os import path to reduce final executable size.
Using UPX
see upx
Encrypting Python Bytecode
To encrypt the Python bytecode modules stored in the bundle, pass the --key=key-string argument on the command line.
For this to work, you must have the PyCrypto module installed. The key-string is a string of 16 characters which is used to encrypt each file of Python byte-code before it is stored in the archive inside the executable file.
| 1 | pip install pycrypto | 
Other similar tools
- py2exe: pip install py2exe
- cx_freeze: pip install cx_freeze
- py2app: pip install py2app
- nuitka from here
Reference
History
- 20181213: created.