Pyinstaller packages Python programs as EXE

The installation

pip install pyinstaller
Copy the code

The whole process

pyi-makespec xxx.py     # Mr. Spec file
pyinstaller xxx.spec    # Regenerate to exe file
Copy the code

Parameters that

-f: Package as an exe file (in dist folder)

pyi-makespec -F xxx.py
pyinstaller xxx.spec
Copy the code

-d: Generates a folder containing exe

pyi-makespec -D xxx.py
pyinstaller xxx.spec
Copy the code

Single PY file

Follow the above steps

Include data files

Open the spec file with a text editor and modify the parameter datas=[] in the file.

For example, if you want to package global_popu.tif into exe

  1. Change the parameters in the PEC file todatas=[('global_popu.tif', '.')]
  2. Then compile the spec file into an exe with two notes:
    1. Put the data files in the same directory as the xxx.py files
    2. The part that reads data from xxx.py program needs to be modified as follows
a = Analysis(['trip_popu.py'],
             pathex=['E:\\GitHub\\Zone\\Zone'],
             binaries=[],
             datas=[('global_popu.tif'.'. ')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
Copy the code
# changes to xxx.py
# Add custom functions
def resource_path(relative_path):    
	base_path = getattr(        
		sys, '_MEIPASS', os.path.dirname(            
			os.path.abspath(__file__)))    
	return os.path.join(base_path, relative_path)
	
#global_popu_Dic = resource_path("global_popu.tif") #
global_popu_Dic = ".. /data/global_popu.tif"			# for debugging
Copy the code

If you want to package two or more data files, modify the datAS in the spec as follows:

datas=[('china_light_2016.tif', '.'),('china_population_2016.tif', '.')]

Multiple PY files

The overall operation is similar to the above, but the key is to modify the parameters in the spec

For example, trip_popu.py in the code folder uses func.py in the zone_func folder,

Add the absolute path to the func.py file to the first parameter of Analysis in the spec file and generate the EXE

a = Analysis(['trip_popu.py'.'E:\\GitHub\\Zone\\Zone\\zone_func\\func.py'],
             pathex=['E:\\GitHub\\Zone\\Zone'],
             binaries=[],
             datas=[('global_popu.tif'.'. ')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
Copy the code

Multi-process packaging

The preparation of a program containing multi-process processing, the normal packaging of the exe, the computer will be stuck

Therefore, before packaging, you need to modify the xxx.py file. The modified content is as follows:

import multiprocessing

if __name__ == "__main__":
    multiprocessing.freeze_support()	# must be placed under if __name__ == "__main__":
Copy the code

After modification, pack it normally

The problem record

  1. The shapely library is used in the program, and geos.dll is missing when it is packaged

    Make a copy of geOS_c. DLL in the shapely library directory and rename it geos. DLL

  2. The program uses the geopandas library and is prompted after running when packing

    File "site-packages\geopandas\datasets\__init__.py", line 7, in <module>
    StopIteration
    [6764] Failed to execute script application
    Copy the code

    Find __init__.py in the geopandas library file and comment out the sentence “import geopandas.datasets”

  3. When an application uses the Sklearn library, the package runtime prompts that some modules, such as sklear.utils._cython_blas, are missing

    After modifying the HiddenImports parameter in the spec file, repackage it as follows

    a = Analysis(['mainarea_buffer.py'.'E:\\GitHub\\Zone\\Zone\\zone_func\\func.py'],
                 pathex=['E:\\GitHub\\Zone\\Zone'],
                 binaries=[],
                 datas=[('global_popu.tif'.'. ')],
                 hiddenimports=['sklearn.utils._cython_blas'.'cython'.'sklearn'.'sklearn.ensemble'.'sklearn.neighbors.typedefs'.'sklearn.neighbors.quad_tree'.'sklearn.tree._utils'.'scipy._lib.messagestream'],
                 hookspath=[],
                 runtime_hooks=[],
                 excludes=[],
                 win_no_prefer_redirects=False,
                 win_private_assemblies=False,
                 cipher=block_cipher,
                 noarchive=False)
    
    Copy the code

reference

Github : github.com/guangmujun

CSDN : me.csdn.net/qq_37054356