2019年3月7日 星期四

[python] 最簡單的方法打包你的 Python 專案, 變成 exe 檔

Create a Python Standalone Application
井民全, Jing, mqjing@gmail.com

請服用 pyinstall

Target Python Source

File: test.py
-------------------------------
import numpy as np

a = [1, 2, 3]
arr = np.asarray(a)
print('arr = ', arr)
-------------------------------

pyinstall

Install pyinstall
pip install pyinstaller

Start package
pyinstaller test.py


問題處理:
1. 處理 recursive depth 不足的問題
Issue: pyinstaller creating EXE RuntimeError: maximum recursion depth exceeded while calling a Python object

Step 1: Generate .spec file
pyinstaller test.py

A file with .spec as extension should be generated

Step 2: Add recursion limit
import sys
sys.setrecursionlimit(5000)


Step 3: package again with the spec file


pyinstaller test.spec


2. Issue : Cannot find existing PyQt5 plugin directories

pip install PyQt5


_cxFreeze



Install cx_Freeze
git clone https://github.com/anthony-tuininga/cx_Freeze.git
# build the tool
python setup.py build

Start package 
File: setup.py
---------------------------------------------------------------------------------------------
import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
#build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}
build_exe_options = {"packages": ["os", "numpy"], "excludes": ["tkinter"]}

# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
    #base = "Win32GUI"
    base = "Console"

setup(  name = "guifoo",
        version = "0.1",
        description = "My GUI application!",
        options = {"build_exe": build_exe_options},
        executables = [Executable("test.py", base=base)])
---------------------------------------------------------------------------------------------

Command
python setup.py build
python setup.py bdist_msi


Run
test.exe


Resolving issue
Error: Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll.

Copy the mkl related dlls next to your exe file
Location: C:\Users\jing\Anaconda3\Library\bin
mkl_core.dll
mkl_def.dll
mkl_intel_thread.dll



Reference
https://cx-freeze.readthedocs.io/en/latest/distutils.html#distutils